2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . IO ;
6
7
using System . Net ;
7
8
using System . Net . Http ;
9
+ using System . Net . Http . Headers ;
8
10
using System . Reflection . Emit ;
9
11
using System . Threading . Tasks ;
10
12
using Newtonsoft . Json ;
@@ -52,9 +54,14 @@ public override async Task BindAsync(BindingContext context)
52
54
// TODO: This logic needs to be made more robust
53
55
// E.g. we might decide to use a Regex to determine if
54
56
// the json is a response body or not
55
- if ( jsonObject [ "status" ] != null && jsonObject [ " body"] != null )
57
+ if ( jsonObject [ "body" ] != null )
56
58
{
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
+
58
65
string body = jsonObject [ "body" ] . ToString ( ) ;
59
66
60
67
response = new HttpResponseMessage ( statusCode ) ;
@@ -65,10 +72,7 @@ public override async Task BindAsync(BindingContext context)
65
72
{
66
73
foreach ( var header in headers )
67
74
{
68
- if ( header . Value != null )
69
- {
70
- response . Headers . Add ( header . Key , header . Value . ToString ( ) ) ;
71
- }
75
+ AddResponseHeader ( response , header ) ;
72
76
}
73
77
}
74
78
}
@@ -106,5 +110,62 @@ public bool CanProcessResult(object result)
106
110
{
107
111
return result is HttpResponseMessage ;
108
112
}
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
+ }
109
170
}
110
171
}
0 commit comments