Commit 387d6618 authored by Honfika's avatar Honfika

throw BodyNotFoundException in Request.Body when it has no body

parent 730d4866
...@@ -136,17 +136,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -136,17 +136,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
private async Task ReadRequestBody() private async Task ReadRequestBody()
{ {
//GET request don't have a request body to read WebSession.Request.EnsureBodyAvailable(false);
if (!WebSession.Request.HasBody)
{
throw new BodyNotFoundException("Request don't have a body. " + "Please verify that this request is a Http POST/PUT/PATCH and request " +
"content length is greater than zero before accessing the body.");
}
if (WebSession.Request.RequestLocked)
{
throw new Exception("You cannot get the request body after request is made to server.");
}
//Caching check //Caching check
if (!WebSession.Request.IsBodyRead) if (!WebSession.Request.IsBodyRead)
......
using System; using System;
using System.Text; using System.Text;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Extensions; using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Shared; using Titanium.Web.Proxy.Shared;
...@@ -181,25 +182,39 @@ namespace Titanium.Web.Proxy.Http ...@@ -181,25 +182,39 @@ namespace Titanium.Web.Proxy.Http
/// </summary> /// </summary>
internal bool CancelRequest { get; set; } internal bool CancelRequest { get; set; }
/// <summary> internal void EnsureBodyAvailable(bool throwWhenNotReadYet = true)
/// Request body as byte array
/// </summary>
public byte[] Body
{ {
get //GET request don't have a request body to read
if (!HasBody)
{ {
if (!IsBodyRead) throw new BodyNotFoundException("Request don't have a body. " + "Please verify that this request is a Http POST/PUT/PATCH and request " +
"content length is greater than zero before accessing the body.");
}
if (!IsBodyRead)
{
if (RequestLocked)
{ {
if (RequestLocked) throw new Exception("You cannot get the request body after request is made to server.");
{ }
throw new Exception("You cannot get the request body after request is made to server.");
}
if (throwWhenNotReadYet)
{
throw new Exception("Request body is not read yet. " + throw new Exception("Request body is not read yet. " +
"Use SessionEventArgs.GetRequestBody() or SessionEventArgs.GetRequestBodyAsString() " + "Use SessionEventArgs.GetRequestBody() or SessionEventArgs.GetRequestBodyAsString() " +
"method to read the request body."); "method to read the request body.");
} }
}
}
/// <summary>
/// Request body as byte array
/// </summary>
public byte[] Body
{
get
{
EnsureBodyAvailable();
return body; return body;
} }
internal set internal set
......
...@@ -168,6 +168,16 @@ namespace Titanium.Web.Proxy.Http ...@@ -168,6 +168,16 @@ namespace Titanium.Web.Proxy.Http
/// </summary> /// </summary>
public HeaderCollection Headers { get; } = new HeaderCollection(); public HeaderCollection Headers { get; } = new HeaderCollection();
internal void EnsureBodyAvailable()
{
if (!IsBodyRead)
{
throw new Exception("Response body is not read yet. " +
"Use SessionEventArgs.GetResponseBody() or SessionEventArgs.GetResponseBodyAsString() " +
"method to read the response body.");
}
}
/// <summary> /// <summary>
/// Response body as byte array /// Response body as byte array
/// </summary> /// </summary>
...@@ -175,13 +185,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -175,13 +185,7 @@ namespace Titanium.Web.Proxy.Http
{ {
get get
{ {
if (!IsBodyRead) EnsureBodyAvailable();
{
throw new Exception("Response body is not read yet. " +
"Use SessionEventArgs.GetResponseBody() or SessionEventArgs.GetResponseBodyAsString() " +
"method to read the response body.");
}
return body; return body;
} }
internal set internal set
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment