22// Licensed under the MIT License. See License.txt in the project root for license information.
33
44using System ;
5+ using System . Collections . Generic ;
56using System . IO ;
67using System . Net ;
78using System . Net . Http ;
9+ using System . Net . Http . Headers ;
810using System . Reflection . Emit ;
911using System . Threading . Tasks ;
1012using Newtonsoft . Json ;
@@ -52,9 +54,14 @@ public override async Task BindAsync(BindingContext context)
5254 // TODO: This logic needs to be made more robust
5355 // E.g. we might decide to use a Regex to determine if
5456 // the json is a response body or not
55- if ( jsonObject [ "status" ] != null && jsonObject [ " body"] != null )
57+ if ( jsonObject [ "body" ] != null )
5658 {
57- HttpStatusCode statusCode = ( HttpStatusCode ) jsonObject . Value < int > ( "status" ) ;
59+ HttpStatusCode statusCode = HttpStatusCode . OK ;
60+ if ( jsonObject [ "status" ] != null )
61+ {
62+ statusCode = ( HttpStatusCode ) jsonObject . Value < int > ( "status" ) ;
63+ }
64+
5865 string body = jsonObject [ "body" ] . ToString ( ) ;
5966
6067 response = new HttpResponseMessage ( statusCode ) ;
@@ -65,10 +72,7 @@ public override async Task BindAsync(BindingContext context)
6572 {
6673 foreach ( var header in headers )
6774 {
68- if ( header . Value != null )
69- {
70- response . Headers . Add ( header . Key , header . Value . ToString ( ) ) ;
71- }
75+ AddResponseHeader ( response , header ) ;
7276 }
7377 }
7478 }
@@ -106,5 +110,62 @@ public bool CanProcessResult(object result)
106110 {
107111 return result is HttpResponseMessage ;
108112 }
113+
114+ private static void AddResponseHeader ( HttpResponseMessage response , KeyValuePair < string , JToken > header )
115+ {
116+ if ( header . Value != null )
117+ {
118+ DateTimeOffset dateTimeOffset ;
119+ switch ( header . Key . ToLowerInvariant ( ) )
120+ {
121+ // The following content headers must be added to the response
122+ // content header collection
123+ case "content-type" :
124+ response . Content . Headers . ContentType = new MediaTypeHeaderValue ( header . Value . ToString ( ) ) ;
125+ break ;
126+ case "content-length" :
127+ long contentLength ;
128+ if ( long . TryParse ( header . Value . ToString ( ) , out contentLength ) )
129+ {
130+ response . Content . Headers . ContentLength = contentLength ;
131+ }
132+ break ;
133+ case "content-disposition" :
134+ response . Content . Headers . ContentDisposition = new ContentDispositionHeaderValue ( header . Value . ToString ( ) ) ;
135+ break ;
136+ case "content-encoding" :
137+ case "content-language" :
138+ case "content-range" :
139+ response . Content . Headers . Add ( header . Key , header . Value . ToString ( ) ) ;
140+ break ;
141+ case "content-location" :
142+ Uri uri ;
143+ if ( Uri . TryCreate ( header . Value . ToString ( ) , UriKind . Absolute , out uri ) )
144+ {
145+ response . Content . Headers . ContentLocation = uri ;
146+ }
147+ break ;
148+ case "content-md5" :
149+ response . Content . Headers . ContentMD5 = header . Value . Value < byte [ ] > ( ) ;
150+ break ;
151+ case "expires" :
152+ if ( DateTimeOffset . TryParse ( header . Value . ToString ( ) , out dateTimeOffset ) )
153+ {
154+ response . Content . Headers . Expires = dateTimeOffset ;
155+ }
156+ break ;
157+ case "last-modified" :
158+ if ( DateTimeOffset . TryParse ( header . Value . ToString ( ) , out dateTimeOffset ) )
159+ {
160+ response . Content . Headers . LastModified = dateTimeOffset ;
161+ }
162+ break ;
163+ default :
164+ // All other headers are added directly to the response
165+ response . Headers . Add ( header . Key , header . Value . ToString ( ) ) ;
166+ break ;
167+ }
168+ }
169+ }
109170 }
110171}
0 commit comments