Commit 3c1c659f authored by Honfika's avatar Honfika

throw BodyNotFoundException in Request.Body when it has no body

parent f342f35d
......@@ -136,17 +136,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
private async Task ReadRequestBody()
{
//GET request don't have a request body to read
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.");
}
WebSession.Request.EnsureBodyAvailable(false);
//Caching check
if (!WebSession.Request.IsBodyRead)
......
using System;
using System.Text;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Shared;
......@@ -181,25 +182,39 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
internal bool CancelRequest { get; set; }
/// <summary>
/// Request body as byte array
/// </summary>
public byte[] Body
internal void EnsureBodyAvailable(bool throwWhenNotReadYet = true)
{
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. " +
"Use SessionEventArgs.GetRequestBody() or SessionEventArgs.GetRequestBodyAsString() " +
"method to read the request body.");
}
}
}
/// <summary>
/// Request body as byte array
/// </summary>
public byte[] Body
{
get
{
EnsureBodyAvailable();
return body;
}
internal set
......
......@@ -168,6 +168,16 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
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>
/// Response body as byte array
/// </summary>
......@@ -175,13 +185,7 @@ namespace Titanium.Web.Proxy.Http
{
get
{
if (!IsBodyRead)
{
throw new Exception("Response body is not read yet. " +
"Use SessionEventArgs.GetResponseBody() or SessionEventArgs.GetResponseBodyAsString() " +
"method to read the response body.");
}
EnsureBodyAvailable();
return body;
}
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