-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add features for parsing form data (#105)
- Loading branch information
1 parent
2a46122
commit ca31d7d
Showing
8 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace Grapevine | ||
{ | ||
public static class HttpRequestExtensions | ||
{ | ||
private static readonly string _multipartContentType = "multipart/form-data; boundary="; | ||
private static readonly int _startIndex = _multipartContentType.Length; | ||
|
||
internal static string GetMultipartBoundary(this IHttpRequest request) | ||
{ | ||
return (string.IsNullOrWhiteSpace(request.ContentType) || !request.ContentType.StartsWith(_multipartContentType)) | ||
? string.Empty | ||
: request.ContentType.Substring(_startIndex); | ||
} | ||
|
||
public static async Task<IDictionary<string, string>> ParseFormUrlEncodedData(this IHttpRequest request) | ||
{ | ||
var data = new Dictionary<string, string>(); | ||
|
||
using (var reader = new StreamReader(request.InputStream, request.ContentEncoding)) | ||
{ | ||
var payload = await reader.ReadToEndAsync(); | ||
|
||
foreach (var kvp in payload.Split('&')) | ||
{ | ||
var pair = kvp.Split('='); | ||
var key = pair[0]; | ||
var value = pair[1]; | ||
|
||
var decoded = string.Empty; | ||
while((decoded = Uri.UnescapeDataString(value)) != value) | ||
{ | ||
value = decoded; | ||
} | ||
|
||
data.Add(key, value); | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Grapevine.Middleware | ||
{ | ||
public static class FormUrlEncodedData | ||
{ | ||
public async static Task Parse(IHttpContext context, IRestServer server) | ||
{ | ||
if (context.Request.ContentType != "application/x-www-form-urlencoded") return; | ||
context.Locals.TryAdd("FormData", await context.Request.ParseFormUrlEncodedData()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Threading.Tasks; | ||
using Grapevine; | ||
using HttpMultipartParser; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Samples.Resources | ||
{ | ||
[RestResource(BasePath = "form")] | ||
public class FormDataResource | ||
{ | ||
private readonly ILogger<FormDataResource> _logger; | ||
|
||
public FormDataResource(ILogger<FormDataResource> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[RestRoute("Post", "/submit/data", Name = "Upload form data", Description = "This demonstrates how to parse application/www-form-urlencoded data.")] | ||
[Header("Content-Type", "application/x-www-form-urlencoded")] | ||
public async Task ParseUrlEncodedFormData(IHttpContext context) | ||
{ | ||
// set a breakpoint here to see the auto-parsed data | ||
await context.Response.SendResponseAsync(HttpStatusCode.Ok); | ||
} | ||
|
||
[RestRoute("Post", "/submit/data", Name = "Upload form data", Description = "This demonstrates how to parse simple multipart/form-data.")] | ||
[Header("Content-Type", "multipart/form-data")] | ||
public async Task ParseMultipartFormData(IHttpContext context) | ||
{ | ||
// /~https://github.com/Http-Multipart-Data-Parser/Http-Multipart-Data-Parser | ||
var content = await MultipartFormDataParser.ParseAsync(context.Request.InputStream, context.Request.MultipartBoundary, context.Request.ContentEncoding); | ||
var name = $"{content.GetParameterValue("FirstName")} {content.GetParameterValue("LastName")} : {content.Files.Count}"; | ||
await context.Response.SendResponseAsync(name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters