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 ...@@ -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,13 +182,15 @@ namespace Titanium.Web.Proxy.Http ...@@ -181,13 +182,15 @@ 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)
{ {
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 (!IsBodyRead)
{ {
if (RequestLocked) if (RequestLocked)
...@@ -195,11 +198,23 @@ namespace Titanium.Web.Proxy.Http ...@@ -195,11 +198,23 @@ namespace Titanium.Web.Proxy.Http
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,12 +168,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -168,12 +168,7 @@ namespace Titanium.Web.Proxy.Http
/// </summary> /// </summary>
public HeaderCollection Headers { get; } = new HeaderCollection(); public HeaderCollection Headers { get; } = new HeaderCollection();
/// <summary> internal void EnsureBodyAvailable()
/// Response body as byte array
/// </summary>
public byte[] Body
{
get
{ {
if (!IsBodyRead) if (!IsBodyRead)
{ {
...@@ -181,7 +176,16 @@ namespace Titanium.Web.Proxy.Http ...@@ -181,7 +176,16 @@ namespace Titanium.Web.Proxy.Http
"Use SessionEventArgs.GetResponseBody() or SessionEventArgs.GetResponseBodyAsString() " + "Use SessionEventArgs.GetResponseBody() or SessionEventArgs.GetResponseBodyAsString() " +
"method to read the response body."); "method to read the response body.");
} }
}
/// <summary>
/// Response body as byte array
/// </summary>
public byte[] Body
{
get
{
EnsureBodyAvailable();
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