forked from NetCoreStack/Proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBodyContentBinder.cs
More file actions
117 lines (100 loc) · 4.5 KB
/
Copy pathBodyContentBinder.cs
File metadata and controls
117 lines (100 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Microsoft.AspNetCore.Http;
using NetCoreStack.Contracts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace NetCoreStack.Proxy
{
public abstract class BodyContentBinder : ContentModelBinder
{
protected IModelSerializer ModelSerializer { get; }
public BodyContentBinder(HttpMethod httpMethod, IModelSerializer modelSerializer)
:base(httpMethod)
{
ModelSerializer = modelSerializer;
}
protected virtual void AddFile(string key, MultipartFormDataContent multipartFormDataContent, IFormFile formFile)
{
using (var ms = new MemoryStream())
{
formFile.CopyTo(ms);
var fileContent = new ByteArrayContent(ms.ToArray());
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(formFile.ContentType);
fileContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition);
multipartFormDataContent.Add(fileContent, key, formFile.FileName);
}
}
protected virtual byte[] Serialize(object value)
{
return Encoding.UTF8.GetBytes(ModelSerializer.Serialize(value));
}
protected virtual StringContent SerializeToString(ContentType contentType, ArraySegment<object> args)
{
var mediaType = "application/json";
if (contentType == ContentType.Xml)
mediaType = "application/xml";
if (args.Count == 1)
{
return new StringContent(ModelSerializer.Serialize(args.First()), Encoding.UTF8, mediaType);
}
return new StringContent(ModelSerializer.Serialize(args), Encoding.UTF8, mediaType);
}
protected virtual MultipartFormDataContent GetMultipartFormDataContent(ModelDictionaryResult contentResult)
{
MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();
foreach (KeyValuePair<string, string> entry in contentResult.Dictionary)
{
multipartFormDataContent.Add(new StringContent(entry.Value), entry.Key);
}
if (contentResult.Files != null)
{
foreach (KeyValuePair<string, IFormFile> entry in contentResult.Files)
{
AddFile(entry.Key, multipartFormDataContent, entry.Value);
}
}
return multipartFormDataContent;
}
protected virtual FormUrlEncodedContent GetUrlEncodedContent(ModelDictionaryResult contentResult)
{
FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(contentResult.Dictionary);
return formUrlEncodedContent;
}
public override void BindContent(ContentModelBindingContext bindingContext)
{
EnsureTemplateResult ensureTemplateResult = EnsureTemplate(bindingContext);
if (ensureTemplateResult.BindingCompleted)
return;
var parameterOffset = ensureTemplateResult.ParameterOffset;
ModelDictionaryResult result = bindingContext.ModelContentResolver.Resolve(bindingContext.Parameters,
bindingContext.Args,
parameterOffset,
ensureTemplateResult.IgnoreModelPrefix);
var contentType = bindingContext.ContentType;
if (contentType == ContentType.MultipartFormData)
{
var content = GetMultipartFormDataContent(result);
bindingContext.ContentResult = ContentModelBindingResult.Success(content);
return;
}
if (contentType == ContentType.FormUrlEncoded)
{
var content = GetUrlEncodedContent(result);
bindingContext.ContentResult = ContentModelBindingResult.Success(content);
return;
}
var remainingParameterCount = bindingContext.ArgsLength - parameterOffset;
if (remainingParameterCount <= 0)
{
// all parameters are resolved as Key - Url
return;
}
var segments = new ArraySegment<object>(bindingContext.Args, parameterOffset, remainingParameterCount);
bindingContext.ContentResult = ContentModelBindingResult.Success(SerializeToString(contentType, segments));
}
}
}