@@ -19,7 +19,7 @@ public class MessageData
19
19
20
20
21
21
public string CorrelationId { get ; }
22
- public string OriginalContentAsString { get ; }
22
+ public string ? OriginalContentAsString { get ; }
23
23
public string HttpString { get ; }
24
24
public string Url { get ; }
25
25
public string Path { get ; }
@@ -36,7 +36,7 @@ public static async Task<MessageData> Create(HttpRequest request, ILogger logger
36
36
return new MessageData ( request , content , logger ) ;
37
37
}
38
38
39
- private MessageData ( HttpRequest request , string contentAsString , ILogger logger )
39
+ private MessageData ( HttpRequest request , string ? contentAsString , ILogger logger )
40
40
{
41
41
_logger = logger ;
42
42
try
@@ -47,8 +47,8 @@ private MessageData(HttpRequest request, string contentAsString, ILogger logger)
47
47
Path = request . Path . HasValue ? request . Path . Value . Trim ( '/' ) : string . Empty ;
48
48
OriginalContentType = RetrieveContentType ( request ) ;
49
49
_headers = request . Headers ;
50
- Url = $ "{ request . Scheme } ://{ request . Host } { request . Path } { request . QueryString } ";
51
- HttpString = $ "{ request . Protocol } { Method } { Url } { OriginalContentType } ";
50
+ Url = $ "{ request . Protocol } ://{ request . Host } { request . Path } { request . QueryString } ";
51
+ HttpString = $ "{ Method } { Url } { request . Protocol . ToUpper ( ) } /1.1 { OriginalContentType } ";
52
52
var correlationId = _headers [ CorrelationIdHeaderName ] . FirstOrDefault ( ) ;
53
53
CorrelationId = string . IsNullOrWhiteSpace ( correlationId ) ? Guid . NewGuid ( ) . ToString ( "D" ) : correlationId ;
54
54
}
@@ -59,37 +59,44 @@ private MessageData(HttpRequest request, string contentAsString, ILogger logger)
59
59
}
60
60
}
61
61
62
- public bool ShouldProcessRequest ( ) => ! ( Method == HttpMethods . Get
63
- && ( Path . Equals ( "health" , StringComparison . CurrentCultureIgnoreCase )
64
- || Path . StartsWith ( "swagger" , StringComparison . CurrentCultureIgnoreCase )
65
- || Path . StartsWith ( CheckRoutesEndpoints . Path , StringComparison . CurrentCultureIgnoreCase ) ) ) ;
62
+ public bool ShouldProcessRequest => ! ( Method == HttpMethods . Get
63
+ && ( Path . Equals ( "health" , StringComparison . InvariantCultureIgnoreCase )
64
+ || Path . StartsWith ( "swagger" , StringComparison . InvariantCultureIgnoreCase )
65
+ || Path . StartsWith ( CheckRoutesEndpoints . Path , StringComparison . InvariantCultureIgnoreCase ) ) ) ;
66
66
67
- public HttpRequestMessage CreateForwardingRequestAsJson ( string ? routeUrl )
67
+ public HttpRequestMessage CreateForwardingRequestAsJson ( string ? routeUrl , string ? hostHeader )
68
68
{
69
69
return OriginalContentType is MediaTypeNames . Application . Xml or MediaTypeNames . Application . Soap
70
- ? CreateForwardingRequest ( routeUrl , string . IsNullOrWhiteSpace ( OriginalContentAsString )
70
+ ? CreateForwardingRequest ( routeUrl , hostHeader , string . IsNullOrWhiteSpace ( OriginalContentAsString )
71
71
? string . Empty
72
72
: XmlToJsonConverter . Convert ( OriginalContentAsString , KnownArrays ) , MediaTypeNames . Application . Json )
73
- : CreateForwardingRequestAsOriginal ( routeUrl ) ;
73
+ : CreateForwardingRequestAsOriginal ( routeUrl , hostHeader ) ;
74
74
}
75
75
76
- public HttpRequestMessage CreateForwardingRequestAsOriginal ( string ? routeUrl )
76
+ public HttpRequestMessage CreateForwardingRequestAsOriginal ( string ? routeUrl , string ? hostHeader )
77
77
{
78
- return CreateForwardingRequest ( routeUrl , OriginalContentAsString , OriginalContentType ) ;
78
+ return CreateForwardingRequest ( routeUrl , hostHeader , OriginalContentAsString , OriginalContentType ) ;
79
79
}
80
80
81
- private HttpRequestMessage CreateForwardingRequest ( string ? routeUrl , string contentAsString , string contentType )
81
+ private HttpRequestMessage CreateForwardingRequest ( string ? routeUrl , string ? hostHeader , string ? contentAsString , string contentType )
82
82
{
83
83
try
84
84
{
85
85
var request = new HttpRequestMessage ( new HttpMethod ( Method ) , routeUrl ) ;
86
- foreach ( var header in _headers . Where ( x => ! x . Key . StartsWith ( "Content-" ) && x . Key != "Host" && x . Key != CorrelationIdHeaderName ) )
86
+ foreach ( var header in _headers . Where ( x => ! x . Key . StartsWith ( "Content-" , StringComparison . InvariantCultureIgnoreCase )
87
+ && ! string . Equals ( x . Key , "Accept" , StringComparison . InvariantCultureIgnoreCase )
88
+ && ! string . Equals ( x . Key , "Host" , StringComparison . InvariantCultureIgnoreCase )
89
+ && ! string . Equals ( x . Key , CorrelationIdHeaderName , StringComparison . InvariantCultureIgnoreCase ) ) )
87
90
request . Headers . Add ( header . Key , header . Value . ToArray ( ) ) ;
88
91
request . Headers . Add ( CorrelationIdHeaderName , CorrelationId ) ;
89
-
90
- request . Content = contentType == MediaTypeNames . Application . Json
91
- ? JsonContent . Create ( JsonNode . Parse ( string . IsNullOrWhiteSpace ( contentAsString ) ? "{}" : contentAsString ) , options : Json . SerializerOptions )
92
- : new StringContent ( contentAsString , Encoding . UTF8 , contentType ) ;
92
+ request . Headers . Add ( "Accept" , contentType ) ;
93
+ if ( ! string . IsNullOrWhiteSpace ( hostHeader ) ) request . Headers . TryAddWithoutValidation ( "host" , hostHeader ) ;
94
+
95
+ request . Content = contentAsString == null || Method == "GET"
96
+ ? null
97
+ : contentType == MediaTypeNames . Application . Json
98
+ ? JsonContent . Create ( JsonNode . Parse ( string . IsNullOrWhiteSpace ( contentAsString ) ? "{}" : contentAsString ) , options : Json . SerializerOptions )
99
+ : new StringContent ( contentAsString , Encoding . UTF8 , contentType ) ;
93
100
94
101
return request ;
95
102
}
@@ -109,7 +116,7 @@ public async Task PopulateResponse(HttpResponse response, RoutingResult routingR
109
116
response . Headers . Date = ( routingResult . ResponseDate ?? DateTimeOffset . Now ) . ToString ( "R" ) ;
110
117
response . Headers [ CorrelationIdHeaderName ] = CorrelationId ;
111
118
response . Headers [ RequestedPathHeaderName ] = routingResult . UrlPath ;
112
- if ( routingResult . ResponseContent != null && response . StatusCode != ( int ) HttpStatusCode . NoContent )
119
+ if ( routingResult . ResponseContent != null && response . StatusCode != ( int ) HttpStatusCode . NoContent )
113
120
await response . BodyWriter . WriteAsync ( new ReadOnlyMemory < byte > ( Encoding . UTF8 . GetBytes ( routingResult . ResponseContent ) ) ) ;
114
121
}
115
122
catch ( Exception ex )
@@ -119,26 +126,29 @@ public async Task PopulateResponse(HttpResponse response, RoutingResult routingR
119
126
}
120
127
}
121
128
122
- private static async Task < string > RetrieveContent ( HttpRequest request )
129
+ private static async Task < string ? > RetrieveContent ( HttpRequest request )
123
130
{
131
+ if ( request . Body == Stream . Null ) return null ;
124
132
request . EnableBuffering ( ) ;
125
133
var content = await new StreamReader ( request . Body ) . ReadToEndAsync ( ) ;
126
134
request . Body . Position = 0 ;
127
135
return content ;
128
136
}
129
137
130
- private string RetrieveContentType ( HttpRequest request )
138
+ private static string RetrieveContentType ( HttpRequest request )
131
139
{
132
140
var contentTypeParts = request . ContentType ? . Split ( ';' ) ;
133
- return contentTypeParts is { Length : > 0 } ? contentTypeParts [ 0 ] : MediaTypeNames . Application . Json ;
141
+ var contentType = contentTypeParts is { Length : > 0 } ? contentTypeParts [ 0 ] : null ;
142
+ if ( request . Headers . Accept . Count > 0 && request . Headers . Accept [ 0 ] != "*/*" ) contentType ??= request . Headers . Accept [ 0 ] ;
143
+ return contentType ?? "" ;
134
144
}
135
145
}
136
146
137
- public partial class ContentMap ( string content )
147
+ public partial class ContentMap ( string ? content )
138
148
{
139
149
[ GeneratedRegex ( "CHED[A-Z]+" ) ] private static partial Regex RegexChed ( ) ;
140
150
[ GeneratedRegex ( "DispatchCountryCode>(.+?)<" ) ] private static partial Regex RegexCountry ( ) ;
141
151
142
- public string ChedType => RegexChed ( ) . Match ( content ) . Value ;
143
- public string CountryCode => RegexCountry ( ) . Match ( content ) . Groups [ 1 ] . Value ;
152
+ public string ? ChedType => content == null ? null : RegexChed ( ) . Match ( content ) . Value ;
153
+ public string ? CountryCode => content == null ? null : RegexCountry ( ) . Match ( content ) . Groups [ 1 ] . Value ;
144
154
}
0 commit comments