/// <summary>
		/// Queues a message for sending in the response stream.
		/// </summary>
		/// <param name="response">The message to send as a response.</param>
		/// <returns>
		/// The pending user agent redirect based message to be sent as an HttpResponse.
		/// </returns>
		/// <remarks>
		/// This method implements spec OAuth V1.0 section 5.3.
		/// </remarks>
		protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response) {
			var webResponse = new OutgoingWebResponse();
			ApplyMessageTemplate(response, webResponse);
			string json = this.SerializeAsJson(response);
			webResponse.SetResponse(json, new ContentType(JsonEncoded));
			return webResponse;
		}
		public void Send() {
			StringWriter writer = new StringWriter();
			HttpRequest httpRequest = new HttpRequest("file", "http://server", string.Empty);
			HttpResponse httpResponse = new HttpResponse(writer);
			HttpContext context = new HttpContext(httpRequest, httpResponse);
			HttpContext.Current = context;

			OutgoingWebResponse response = new OutgoingWebResponse();
			response.Status = System.Net.HttpStatusCode.OK;
			response.Headers["someHeaderName"] = "someHeaderValue";
			response.Body = "some body";
			response.Send();
			string results = writer.ToString();
			// For some reason the only output in test is the body... the headers require a web host
			Assert.AreEqual(response.Body, results);
		}
		public void SetBodyToByteStream() {
			var response = new OutgoingWebResponse();
			string stringValue = "abc";
			response.Body = stringValue;
			Assert.AreEqual(stringValue.Length, response.ResponseStream.Length);

			// Verify that the actual bytes are correct.
			Encoding encoding = new UTF8Encoding(false); // avoid emitting a byte-order mark
			var expectedBuffer = encoding.GetBytes(stringValue);
			var actualBuffer = new byte[stringValue.Length];
			Assert.AreEqual(stringValue.Length, response.ResponseStream.Read(actualBuffer, 0, stringValue.Length));
			CollectionAssert.AreEqual(expectedBuffer, actualBuffer);

			// Verify that the header was set correctly.
			Assert.IsNull(response.Headers[HttpResponseHeader.ContentEncoding]);
			Assert.AreEqual(encoding.HeaderName, new ContentType(response.Headers[HttpResponseHeader.ContentType]).CharSet);
		}
		public void AsHttpResponseMessage() {
			var responseContent = new byte[10];
			(new Random()).NextBytes(responseContent);
			var responseStream = new MemoryStream(responseContent);
			var outgoingResponse = new OutgoingWebResponse();
			outgoingResponse.Headers.Add("X-SOME-HEADER", "value");
			outgoingResponse.Headers.Add("Content-Length", responseContent.Length.ToString(CultureInfo.InvariantCulture));
			outgoingResponse.ResponseStream = responseStream;

			var httpResponseMessage = outgoingResponse.AsHttpResponseMessage();
			Assert.That(httpResponseMessage, Is.Not.Null);
			Assert.That(httpResponseMessage.Headers.GetValues("X-SOME-HEADER").ToList(), Is.EqualTo(new[] { "value" }));
			Assert.That(
				httpResponseMessage.Content.Headers.GetValues("Content-Length").ToList(),
				Is.EqualTo(new[] { responseContent.Length.ToString(CultureInfo.InvariantCulture) }));
			var actualContent = new byte[responseContent.Length + 1]; // give the opportunity to provide a bit more data than we expect.
			var bytesRead = httpResponseMessage.Content.ReadAsStreamAsync().Result.Read(actualContent, 0, actualContent.Length);
			Assert.That(bytesRead, Is.EqualTo(responseContent.Length)); // verify that only the data we expected came back.
			var trimmedActualContent = new byte[bytesRead];
			Array.Copy(actualContent, trimmedActualContent, bytesRead);
			Assert.That(trimmedActualContent, Is.EqualTo(responseContent));
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="OutgoingWebResponseActionResult"/> class.
		/// </summary>
		/// <param name="response">The response.</param>
		internal OutgoingWebResponseActionResult(OutgoingWebResponse response) {
			Requires.NotNull(response, "response");
			this.response = response;
		}
Exemple #6
0
		/// <summary>
		/// Queues a message for sending in the response stream where the fields
		/// are sent in the response stream in querystring style.
		/// </summary>
		/// <param name="response">The message to send as a response.</param>
		/// <returns>
		/// The pending user agent redirect based message to be sent as an HttpResponse.
		/// </returns>
		/// <remarks>
		/// This method implements spec V1.0 section 5.3.
		/// </remarks>
		protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response) {
			var messageAccessor = this.MessageDescriptions.GetAccessor(response);
			var fields = messageAccessor.Serialize();
			byte[] keyValueEncoding = KeyValueFormEncoding.GetBytes(fields);

			OutgoingWebResponse preparedResponse = new OutgoingWebResponse();
			preparedResponse.Headers.Add(HttpResponseHeader.ContentType, KeyValueFormContentType);
			preparedResponse.OriginalMessage = response;
			preparedResponse.ResponseStream = new MemoryStream(keyValueEncoding);

			IHttpDirectResponse httpMessage = response as IHttpDirectResponse;
			if (httpMessage != null) {
				preparedResponse.Status = httpMessage.HttpStatusCode;
			}

			return preparedResponse;
		}
Exemple #7
0
		/// <summary>
		/// Queues a message for sending in the response stream where the fields
		/// are sent in the response stream in querystring style.
		/// </summary>
		/// <param name="response">The message to send as a response.</param>
		/// <returns>The pending user agent redirect based message to be sent as an HttpResponse.</returns>
		/// <remarks>
		/// This method implements spec V1.0 section 5.3.
		/// </remarks>
		protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response) {
			var messageAccessor = this.MessageDescriptions.GetAccessor(response);
			var fields = messageAccessor.Serialize();
			string responseBody = MessagingUtilities.CreateQueryString(fields);

			OutgoingWebResponse encodedResponse = new OutgoingWebResponse {
				Body = responseBody,
				OriginalMessage = response,
				Status = HttpStatusCode.OK,
				Headers = new System.Net.WebHeaderCollection(),
			};

			ApplyMessageTemplate(response, encodedResponse);
			return encodedResponse;
		}
 public RedirectResponse(OutgoingWebResponse response)
 {
     Response = response;
 }
 public OutgoingRequestActionResult(OutgoingWebResponse response)
 {
     this._response = response;
 }
Exemple #10
0
 public static void OpenAuthResponseToHttp(OSHttpResponse httpResponse, OutgoingWebResponse openAuthResponse)
 {
     httpResponse.StatusCode = (int)openAuthResponse.Status;
     foreach (string key in openAuthResponse.Headers.Keys)
         httpResponse.AddHeader(key, openAuthResponse.Headers[key]);
     if (!String.IsNullOrEmpty(openAuthResponse.Body))
         AddToBody(httpResponse, openAuthResponse.Body);
 }
Exemple #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OutgoingWebResponseActionResult"/> class.
 /// </summary>
 /// <param name="response">The response.</param>
 internal OutgoingWebResponseActionResult(OutgoingWebResponse response)
 {
     Requires.NotNull(response, "response");
     this.response = response;
 }
		/// <summary>
		/// Initializes a new instance of the <see cref="OutgoingWebResponseActionResult"/> class.
		/// </summary>
		/// <param name="response">The response.</param>
		internal OutgoingWebResponseActionResult(OutgoingWebResponse response) {
			Contract.Requires<ArgumentNullException>(response != null);
			this.response = response;
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="OutgoingWebResponseActionResult"/> class.
 /// </summary>
 /// <param name="response">The response.</param>
 internal OutgoingWebResponseActionResult(OutgoingWebResponse response)
 {
     Contract.Requires <ArgumentNullException>(response != null);
     this.response = response;
 }
Exemple #14
0
        /*public static void OpenAuthResponseToHttp(OSHttpResponse httpResponse, OutgoingWebResponse openAuthResponse)
        {
            httpResponse.StatusCode = (int)openAuthResponse.Status;
            foreach (string key in openAuthResponse.Headers.Keys)
                httpResponse.AddHeader(key, openAuthResponse.Headers[key]);
            if (!String.IsNullOrEmpty(openAuthResponse.Body))
                AddToBody(httpResponse, openAuthResponse.Body);
        }*/

        public static byte[] MakeOpenAuthResponse(OSHttpResponse httpResponse, OutgoingWebResponse openAuthResponse)
        {
            httpResponse.StatusCode = (int)openAuthResponse.Status;
            foreach (string key in openAuthResponse.Headers.Keys)
                httpResponse.AddHeader(key, openAuthResponse.Headers[key]);
            if (!String.IsNullOrEmpty(openAuthResponse.Body))
                return Encoding.UTF8.GetBytes(openAuthResponse.Body);
            else
                return Utils.EmptyBytes;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="OutgoingWebResponseActionResult"/> class.
 /// </summary>
 /// <param name="response">The response.</param>
 internal OutgoingWebResponseActionResult(OutgoingWebResponse response)
 {
     Contract.Requires(response != null);
     ErrorUtilities.VerifyArgumentNotNull(response, "response");
     this.response = response;
 }
		/// <summary>
		/// Queues a message for sending in the response stream where the fields
		/// are sent in the response stream in querystring style.
		/// </summary>
		/// <param name="response">The message to send as a response.</param>
		/// <returns>
		/// The pending user agent redirect based message to be sent as an HttpResponse.
		/// </returns>
		/// <remarks>
		/// This method implements spec OAuth V1.0 section 5.3.
		/// </remarks>
		protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response) {
			var webResponse = new OutgoingWebResponse();

			// The only direct response from a resource server is a 401 Unauthorized error.
			var unauthorizedResponse = response as UnauthorizedResponse;
			ErrorUtilities.VerifyInternal(unauthorizedResponse != null, "Only unauthorized responses are expected.");

			// First initialize based on the specifics within the message.
			this.ApplyMessageTemplate(response, webResponse);
			if (!(response is IHttpDirectResponse)) {
				webResponse.Status = HttpStatusCode.Unauthorized;
			}

			// Now serialize all the message parts into the WWW-Authenticate header.
			var fields = this.MessageDescriptions.GetAccessor(response);
			webResponse.Headers[HttpResponseHeader.WwwAuthenticate] = MessagingUtilities.AssembleAuthorizationHeader(Protocol.BearerHttpAuthorizationScheme, fields);
			return webResponse;
		}
Exemple #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OutgoingWebResponseActionResult"/> class.
 /// </summary>
 /// <param name="response">The response.</param>
 internal InternalOutgoingWebResponseActionResult(OutgoingWebResponse response)
 {
     this.response = response;
 }
        /// <summary>
        /// Queues a message for sending in the response stream where the fields
        /// are sent in the response stream in querystring style.
        /// </summary>
        /// <param name="response">The message to send as a response.</param>
        /// <returns>The pending user agent redirect based message to be sent as an HttpResponse.</returns>
        /// <remarks>
        /// This method implements spec V1.0 section 5.3.
        /// </remarks>
        protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response)
        {
            ErrorUtilities.VerifyArgumentNotNull(response, "response");

            var messageAccessor = this.MessageDescriptions.GetAccessor(response);
            var fields = messageAccessor.Serialize();
            string responseBody = MessagingUtilities.CreateQueryString(fields);

            OutgoingWebResponse encodedResponse = new OutgoingWebResponse {
                Body = responseBody,
                OriginalMessage = response,
                Status = HttpStatusCode.OK,
                Headers = new System.Net.WebHeaderCollection(),
            };

            IHttpDirectResponse httpMessage = response as IHttpDirectResponse;
            if (httpMessage != null) {
                encodedResponse.Status = httpMessage.HttpStatusCode;
            }

            return encodedResponse;
        }
		public void AsHttpResponseMessageNoContent() {
			var outgoingResponse = new OutgoingWebResponse();
			outgoingResponse.Headers.Add("X-SOME-HEADER", "value");

			var httpResponseMessage = outgoingResponse.AsHttpResponseMessage();
			Assert.That(httpResponseMessage, Is.Not.Null);
			Assert.That(httpResponseMessage.Headers.GetValues("X-SOME-HEADER").ToList(), Is.EqualTo(new[] { "value" }));
			Assert.That(httpResponseMessage.Content, Is.Null);
		}
 /// <summary>
 /// Transforms an OutgoingWebResponse to an MVC-friendly ActionResult.
 /// </summary>
 /// <param name="response">The response to send to the uesr agent.</param>
 /// <returns>The <see cref="ActionResult"/> instance to be returned by the Controller's action method.</returns>
 public static ActionResult AsActionResult(this OutgoingWebResponse response)
 {
     Contract.Requires <ArgumentNullException>(response != null);
     return(new OutgoingWebResponseActionResult(response));
 }