Skip to content

Use XDocument.Save or XDocument.SaveAsync to write to response body when possible #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Twilio.AspNet.Core/MinimalApiTwiMLResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ public partial class TwiMLResult : IResult
/// Writes the TwiML to the HTTP response body
/// </summary>
/// <param name="httpContext">The HttpContext containing the Response to write the TwiML to</param>
public Task ExecuteAsync(HttpContext httpContext) => WriteTwiMLToResponse(httpContext.Response);
public Task ExecuteAsync(HttpContext httpContext)
=> WriteTwiMLToResponse(httpContext.Response, httpContext.RequestAborted);
}
28 changes: 19 additions & 9 deletions src/Twilio.AspNet.Core/TwiMLResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -17,7 +18,7 @@ public partial class TwiMLResult : IActionResult
public TwiMLResult(TwiML.TwiML twiml) : this(twiml, SaveOptions.None)
{
}

/// <param name="twiml">The TwiML to respond with</param>
/// <param name="formattingOptions">Specifies how to format TwiML</param>
public TwiMLResult(TwiML.TwiML twiml, SaveOptions formattingOptions)
Expand All @@ -29,20 +30,29 @@ public TwiMLResult(TwiML.TwiML twiml, SaveOptions formattingOptions)
public async Task ExecuteResultAsync(ActionContext actionContext)
{
var response = actionContext.HttpContext.Response;
await WriteTwiMLToResponse(response);
await WriteTwiMLToResponse(response, actionContext.HttpContext.RequestAborted)
.ConfigureAwait(false);
}
private async Task WriteTwiMLToResponse(HttpResponse response)

private async Task WriteTwiMLToResponse(HttpResponse response, CancellationToken cancellationToken)
{
response.ContentType = "application/xml";
if (twiml == null)
{
await response.WriteAsync("<Response></Response>");
await response.WriteAsync("<Response></Response>", cancellationToken)
.ConfigureAwait(false);
return;
}

var data = twiml.ToString(formattingOptions);
await response.WriteAsync(data);
var doc = twiml.ToXDocument();

#if NET5_0_OR_GREATER
await doc.SaveAsync(response.Body, formattingOptions, cancellationToken)
.ConfigureAwait(false);
#else
await response.WriteAsync(doc.ToString(formattingOptions), cancellationToken)
.ConfigureAwait(false);
#endif
}
}
}
}
2 changes: 1 addition & 1 deletion src/Twilio.AspNet.Core/ValidateRequestAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
var httpContext = filterContext.HttpContext;
var request = httpContext.Request;
string urlOverride = null;
if (BaseUrlOverride != null)
if (!string.IsNullOrEmpty(BaseUrlOverride))
{
urlOverride = $"{BaseUrlOverride}{request.Path}{request.QueryString}";
}
Expand Down
8 changes: 4 additions & 4 deletions src/Twilio.AspNet.Core/ValidateTwilioRequestFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ public ValidateTwilioRequestFilter(IServiceProvider serviceProvider)
AllowLocal = options.AllowLocal ?? true;
}

public async ValueTask<object> InvokeAsync(
public ValueTask<object> InvokeAsync(
EndpointFilterInvocationContext efiContext,
EndpointFilterDelegate next
)
{
var httpContext = efiContext.HttpContext;
var request = httpContext.Request;
string urlOverride = null;
if (BaseUrlOverride != null)
if (!string.IsNullOrEmpty(BaseUrlOverride))
{
urlOverride = $"{BaseUrlOverride}{request.Path}{request.QueryString}";
}

if (RequestValidationHelper.IsValidRequest(httpContext, AuthToken, urlOverride, AllowLocal))
{
return await next(efiContext);
return next(efiContext);
}

return Results.StatusCode((int) HttpStatusCode.Forbidden);
return ValueTask.FromResult((object)Results.StatusCode((int) HttpStatusCode.Forbidden));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Twilio.AspNet.Core/ValidateTwilioRequestMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ IOptions<TwilioRequestValidationOptions> options
this.options = options.Value ?? throw new Exception("RequestValidationOptions is not configured.");
}

public async Task InvokeAsync(HttpContext context)
public Task InvokeAsync(HttpContext context)
{
var request = context.Request;

string urlOverride = null;
if (options.BaseUrlOverride != null)
if (!string.IsNullOrEmpty(options.BaseUrlOverride))
{
urlOverride = $"{options.BaseUrlOverride.TrimEnd('/')}{request.Path}{request.QueryString}";
}
Expand All @@ -38,10 +38,10 @@ public async Task InvokeAsync(HttpContext context)
if (!isValid)
{
context.Response.StatusCode = (int) HttpStatusCode.Forbidden;
return;
return Task.CompletedTask;
}

await next(context);
return next(context);
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/Twilio.AspNet.Mvc/TwiMLResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ public override void ExecuteResult(ControllerContext controllerContext)
return;
}

var twimlString = dataTwiml.ToString(formattingOptions);
if (encoding != "utf-8")
{
twimlString = twimlString.Replace("utf-8", encoding);
}

response.Output.Write(twimlString);
var doc = dataTwiml.ToXDocument();
doc.Save(response.Output, formattingOptions);
}
}
}
2 changes: 1 addition & 1 deletion src/Twilio.AspNet.Mvc/ValidateRequestAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var httpContext = filterContext.HttpContext;
string urlOverride = null;
if (BaseUrlOverride != null)
if (!string.IsNullOrEmpty(BaseUrlOverride))
{
urlOverride = $"{BaseUrlOverride}{httpContext.Request.Path}{httpContext.Request.QueryString}";
}
Expand Down
1 change: 0 additions & 1 deletion src/testapps/AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
Expand Down