11using Microsoft . AspNetCore . Http ;
2- using Newtonsoft . Json ;
2+ using NetCoreStack . Contracts ;
3+ using System ;
34using System . Collections . Generic ;
45using System . IO ;
6+ using System . Linq ;
57using System . Net . Http ;
68using System . Net . Http . Headers ;
79using System . Text ;
@@ -10,6 +12,14 @@ namespace NetCoreStack.Proxy
1012{
1113 public abstract class BodyContentBinder : ContentModelBinder
1214 {
15+ protected IModelSerializer ModelSerializer { get ; }
16+
17+ public BodyContentBinder ( HttpMethod httpMethod , IModelSerializer modelSerializer )
18+ : base ( httpMethod )
19+ {
20+ ModelSerializer = modelSerializer ;
21+ }
22+
1323 protected virtual void AddFile ( string key , MultipartFormDataContent multipartFormDataContent , IFormFile formFile )
1424 {
1525 using ( var ms = new MemoryStream ( ) )
@@ -24,12 +34,21 @@ protected virtual void AddFile(string key, MultipartFormDataContent multipartFor
2434
2535 protected virtual byte [ ] Serialize ( object value )
2636 {
27- return Encoding . UTF8 . GetBytes ( JsonConvert . SerializeObject ( value ) ) ;
37+ return Encoding . UTF8 . GetBytes ( ModelSerializer . Serialize ( value ) ) ;
2838 }
2939
30- protected virtual StringContent SerializeToString ( object value )
40+ protected virtual StringContent SerializeToString ( ContentType contentType , ArraySegment < object > args )
3141 {
32- return new StringContent ( JsonConvert . SerializeObject ( value ) , Encoding . UTF8 , "application/json" ) ;
42+ var mediaType = "application/json" ;
43+ if ( contentType == ContentType . Xml )
44+ mediaType = "application/xml" ;
45+
46+ if ( args . Count == 1 )
47+ {
48+ return new StringContent ( ModelSerializer . Serialize ( args . First ( ) ) , Encoding . UTF8 , mediaType ) ;
49+ }
50+
51+ return new StringContent ( ModelSerializer . Serialize ( args ) , Encoding . UTF8 , mediaType ) ;
3352 }
3453
3554 protected virtual MultipartFormDataContent GetMultipartFormDataContent ( ModelDictionaryResult contentResult )
@@ -51,5 +70,48 @@ protected virtual MultipartFormDataContent GetMultipartFormDataContent(ModelDict
5170 return multipartFormDataContent ;
5271 }
5372
73+ protected virtual FormUrlEncodedContent GetUrlEncodedContent ( ModelDictionaryResult contentResult )
74+ {
75+ FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent ( contentResult . Dictionary ) ;
76+ return formUrlEncodedContent ;
77+ }
78+
79+ public override void BindContent ( ContentModelBindingContext bindingContext )
80+ {
81+ EnsureTemplateResult ensureTemplateResult = EnsureTemplate ( bindingContext ) ;
82+ if ( ensureTemplateResult . BindingCompleted )
83+ return ;
84+
85+ var parameterOffset = ensureTemplateResult . ParameterOffset ;
86+ ModelDictionaryResult result = bindingContext . ModelContentResolver . Resolve ( bindingContext . Parameters ,
87+ bindingContext . Args ,
88+ parameterOffset ,
89+ ensureTemplateResult . IgnoreModelPrefix ) ;
90+
91+ var contentType = bindingContext . ContentType ;
92+ if ( contentType == ContentType . MultipartFormData )
93+ {
94+ var content = GetMultipartFormDataContent ( result ) ;
95+ bindingContext . ContentResult = ContentModelBindingResult . Success ( content ) ;
96+ return ;
97+ }
98+
99+ if ( contentType == ContentType . FormUrlEncoded )
100+ {
101+ var content = GetUrlEncodedContent ( result ) ;
102+ bindingContext . ContentResult = ContentModelBindingResult . Success ( content ) ;
103+ return ;
104+ }
105+
106+ var remainingParameterCount = bindingContext . ArgsLength - parameterOffset ;
107+ if ( remainingParameterCount <= 0 )
108+ {
109+ // all parameters are resolved as Key - Url
110+ return ;
111+ }
112+
113+ var segments = new ArraySegment < object > ( bindingContext . Args , parameterOffset , remainingParameterCount ) ;
114+ bindingContext . ContentResult = ContentModelBindingResult . Success ( SerializeToString ( contentType , segments ) ) ;
115+ }
54116 }
55- }
117+ }
0 commit comments