Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
89e599a3
Commit
89e599a3
authored
Sep 22, 2017
by
yangxiaodong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update gateway.
parent
cf2512b4
Changes
15
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
322 additions
and
74 deletions
+322
-74
DownstreamUrl.cs
src/DotNetCore.CAP/Dashboard/GatewayProxy/DownstreamUrl.cs
+2
-6
GatewayProxyMiddleware.cs
...Core.CAP/Dashboard/GatewayProxy/GatewayProxyMiddleware.cs
+45
-24
GatewayProxyMiddlewareBase.cs
....CAP/Dashboard/GatewayProxy/GatewayProxyMiddlewareBase.cs
+39
-0
HostAndPort.cs
src/DotNetCore.CAP/Dashboard/GatewayProxy/HostAndPort.cs
+2
-6
IRequestMapper.Default.cs
...Core.CAP/Dashboard/GatewayProxy/IRequestMapper.Default.cs
+147
-0
IRequestMapper.cs
src/DotNetCore.CAP/Dashboard/GatewayProxy/IRequestMapper.cs
+11
-0
IRequestScopedDataRepository.HttpData.cs
...ard/GatewayProxy/IRequestScopedDataRepository.HttpData.cs
+56
-0
IRequestScopedDataRepository.cs
...AP/Dashboard/GatewayProxy/IRequestScopedDataRepository.cs
+2
-3
HttpClientBuilder.cs
...CAP/Dashboard/GatewayProxy/Requester/HttpClientBuilder.cs
+6
-8
HttpClientHttpRequester.cs
...shboard/GatewayProxy/Requester/HttpClientHttpRequester.cs
+0
-2
IHttpClient.cs
...tCore.CAP/Dashboard/GatewayProxy/Requester/IHttpClient.cs
+2
-3
IHttpClientBuilder.cs
...AP/Dashboard/GatewayProxy/Requester/IHttpClientBuilder.cs
+2
-9
IHttpClientCache.cs
....CAP/Dashboard/GatewayProxy/Requester/IHttpClientCache.cs
+4
-5
IHttpRequester.cs
...re.CAP/Dashboard/GatewayProxy/Requester/IHttpRequester.cs
+1
-1
MemoryHttpClientCache.cs
...Dashboard/GatewayProxy/Requester/MemoryHttpClientCache.cs
+3
-7
No files found.
src/DotNetCore.CAP/Dashboard/GatewayProxy/DownstreamUrl.cs
View file @
89e599a3
using
System
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
using
System.Collections.Generic
;
using
System.Text
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
{
{
public
class
DownstreamUrl
public
class
DownstreamUrl
{
{
...
...
src/DotNetCore.CAP/Dashboard/GatewayProxy/GatewayProxyMiddleware.cs
View file @
89e599a3
using
System.Collections.Generic
;
using
System
;
using
System.Net.Http
;
using
System.Threading.Tasks
;
using
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
;
using
Microsoft.AspNetCore.Http
;
using
Microsoft.Extensions.Logging
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
{
{
public
abstract
class
GatewayProxyMiddlewar
e
public
class
GatewayProxyMiddleware
:
GatewayProxyMiddlewareBas
e
{
{
private
readonly
IRequestScopedDataRepository
_requestScopedDataRepository
;
private
readonly
RequestDelegate
_next
;
private
readonly
ILogger
_logger
;
private
readonly
IRequestMapper
_requestMapper
;
private
readonly
IHttpRequester
_requester
;
public
GatewayProxyMiddleware
(
RequestDelegate
next
,
ILoggerFactory
loggerFactory
,
IRequestMapper
requestMapper
,
IHttpRequester
requester
)
{
_next
=
next
;
_logger
=
loggerFactory
.
CreateLogger
<
GatewayProxyMiddleware
>();
_requestMapper
=
requestMapper
;
_requester
=
requester
;
}
p
rotected
GatewayProxyMiddleware
(
IRequestScopedDataRepository
requestScopedDataRepository
)
p
ublic
async
Task
Invoke
(
HttpContext
context
,
IRequestScopedDataRepository
requestScopedDataRepository
)
{
{
_requestScopedDataRepository
=
requestScopedDataRepository
;
_requestScopedDataRepository
=
requestScopedDataRepository
;
MiddlewareName
=
this
.
GetType
().
Name
;
}
public
string
MiddlewareName
{
get
;
}
_logger
.
LogDebug
(
"started calling gateway proxy middleware"
);
//public DownstreamRoute DownstreamRoute => _requestScopedDataRepository.Get<DownstreamRoute>("DownstreamRoute"
);
var
downstreamRequest
=
await
_requestMapper
.
Map
(
context
.
Request
);
public
HttpRequestMessage
Request
=>
_requestScopedDataRepository
.
Get
<
HttpRequestMessage
>(
"R
equest"
);
_logger
.
LogDebug
(
"setting downstream r
equest"
);
public
HttpRequestMessage
DownstreamRequest
=>
_requestScopedDataRepository
.
Get
<
HttpRequestMessage
>(
"DownstreamRequest"
);
SetDownstreamRequest
(
downstreamRequest
);
public
HttpResponseMessage
HttpResponseMessage
=>
_requestScopedDataRepository
.
Get
<
HttpResponseMessage
>(
"HttpResponseMessage
"
);
_logger
.
LogDebug
(
"setting upstream request
"
);
public
void
SetUpstreamRequestForThisRequest
(
HttpRequestMessage
request
)
SetUpstreamRequestForThisRequest
(
DownstreamRequest
);
{
_requestScopedDataRepository
.
Add
(
"Request"
,
request
);
}
public
void
SetDownstreamRequest
(
HttpRequestMessage
request
)
var
uriBuilder
=
new
UriBuilder
(
DownstreamRequest
.
RequestUri
)
{
{
_requestScopedDataRepository
.
Add
(
"DownstreamRequest"
,
request
);
//Path = dsPath.Data.Value,
}
//Scheme = DownstreamRoute.ReRoute.DownstreamScheme
};
public
void
SetHttpResponseMessageThisRequest
(
HttpResponseMessage
responseMessage
)
DownstreamRequest
.
RequestUri
=
uriBuilder
.
Uri
;
{
_requestScopedDataRepository
.
Add
(
"HttpResponseMessage"
,
responseMessage
);
_logger
.
LogDebug
(
"started calling request"
);
var
response
=
await
_requester
.
GetResponse
(
Request
);
_logger
.
LogDebug
(
"setting http response message"
);
SetHttpResponseMessageThisRequest
(
response
);
_logger
.
LogDebug
(
"returning to calling middleware"
);
}
}
}
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/Dashboard/GatewayProxy/GatewayProxyMiddlewareBase.cs
0 → 100644
View file @
89e599a3
using
System.Net.Http
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
{
public
abstract
class
GatewayProxyMiddlewareBase
{
protected
IRequestScopedDataRepository
_requestScopedDataRepository
;
protected
GatewayProxyMiddlewareBase
()
{
MiddlewareName
=
this
.
GetType
().
Name
;
}
public
string
MiddlewareName
{
get
;
}
//public DownstreamRoute DownstreamRoute => _requestScopedDataRepository.Get<DownstreamRoute>("DownstreamRoute");
public
HttpRequestMessage
Request
=>
_requestScopedDataRepository
.
Get
<
HttpRequestMessage
>(
"Request"
);
public
HttpRequestMessage
DownstreamRequest
=>
_requestScopedDataRepository
.
Get
<
HttpRequestMessage
>(
"DownstreamRequest"
);
public
HttpResponseMessage
HttpResponseMessage
=>
_requestScopedDataRepository
.
Get
<
HttpResponseMessage
>(
"HttpResponseMessage"
);
public
void
SetUpstreamRequestForThisRequest
(
HttpRequestMessage
request
)
{
_requestScopedDataRepository
.
Add
(
"Request"
,
request
);
}
public
void
SetDownstreamRequest
(
HttpRequestMessage
request
)
{
_requestScopedDataRepository
.
Add
(
"DownstreamRequest"
,
request
);
}
public
void
SetHttpResponseMessageThisRequest
(
HttpResponseMessage
responseMessage
)
{
_requestScopedDataRepository
.
Add
(
"HttpResponseMessage"
,
responseMessage
);
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/Dashboard/GatewayProxy/HostAndPort.cs
View file @
89e599a3
using
System
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
using
System.Collections.Generic
;
using
System.Text
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
{
{
public
class
HostAndPort
public
class
HostAndPort
{
{
...
...
src/DotNetCore.CAP/Dashboard/GatewayProxy/IRequestMapper.Default.cs
0 → 100644
View file @
89e599a3
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Linq
;
using
System.Net.Http
;
using
System.Text
;
using
System.Threading.Tasks
;
using
Microsoft.AspNetCore.Http
;
using
Microsoft.Extensions.Primitives
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
{
public
class
RequestMapper
:
IRequestMapper
{
private
readonly
string
[]
_unsupportedHeaders
=
{
"host"
};
private
const
string
SchemeDelimiter
=
"://"
;
public
async
Task
<
HttpRequestMessage
>
Map
(
HttpRequest
request
)
{
try
{
var
requestMessage
=
new
HttpRequestMessage
()
{
Content
=
await
MapContent
(
request
),
Method
=
MapMethod
(
request
),
RequestUri
=
MapUri
(
request
)
};
MapHeaders
(
request
,
requestMessage
);
return
requestMessage
;
}
catch
(
Exception
ex
)
{
throw
new
Exception
(
$"Error when parsing incoming request, exception:
{
ex
.
Message
}
"
);
}
}
private
async
Task
<
HttpContent
>
MapContent
(
HttpRequest
request
)
{
if
(
request
.
Body
==
null
)
{
return
null
;
}
var
content
=
new
ByteArrayContent
(
await
ToByteArray
(
request
.
Body
));
content
.
Headers
.
TryAddWithoutValidation
(
"Content-Type"
,
new
[]
{
request
.
ContentType
});
return
content
;
}
private
HttpMethod
MapMethod
(
HttpRequest
request
)
{
return
new
HttpMethod
(
request
.
Method
);
}
private
Uri
MapUri
(
HttpRequest
request
)
{
return
new
Uri
(
GetEncodedUrl
(
request
));
}
private
void
MapHeaders
(
HttpRequest
request
,
HttpRequestMessage
requestMessage
)
{
foreach
(
var
header
in
request
.
Headers
)
{
//todo get rid of if..
if
(
IsSupportedHeader
(
header
))
{
requestMessage
.
Headers
.
TryAddWithoutValidation
(
header
.
Key
,
header
.
Value
.
ToArray
());
}
}
}
private
async
Task
<
byte
[
]>
ToByteArray
(
Stream
stream
)
{
using
(
stream
)
{
using
(
var
memStream
=
new
MemoryStream
())
{
await
stream
.
CopyToAsync
(
memStream
);
return
memStream
.
ToArray
();
}
}
}
private
bool
IsSupportedHeader
(
KeyValuePair
<
string
,
StringValues
>
header
)
{
return
!
_unsupportedHeaders
.
Contains
(
header
.
Key
.
ToLower
());
}
/// <summary>
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
/// Note that unicode in the HostString will be encoded as punycode.
/// </summary>
/// <param name="scheme">http, https, etc.</param>
/// <param name="host">The host portion of the uri normally included in the Host header. This may include the port.</param>
/// <param name="pathBase">The first portion of the request path associated with application root.</param>
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
/// <param name="query">The query, if any.</param>
/// <param name="fragment">The fragment, if any.</param>
/// <returns></returns>
public
string
BuildAbsolute
(
string
scheme
,
HostString
host
,
PathString
pathBase
=
new
PathString
(),
PathString
path
=
new
PathString
(),
QueryString
query
=
new
QueryString
(),
FragmentString
fragment
=
new
FragmentString
())
{
if
(
scheme
==
null
)
{
throw
new
ArgumentNullException
(
nameof
(
scheme
));
}
var
combinedPath
=
(
pathBase
.
HasValue
||
path
.
HasValue
)
?
(
pathBase
+
path
).
ToString
()
:
"/"
;
var
encodedHost
=
host
.
ToString
();
var
encodedQuery
=
query
.
ToString
();
var
encodedFragment
=
fragment
.
ToString
();
// PERF: Calculate string length to allocate correct buffer size for StringBuilder.
var
length
=
scheme
.
Length
+
SchemeDelimiter
.
Length
+
encodedHost
.
Length
+
combinedPath
.
Length
+
encodedQuery
.
Length
+
encodedFragment
.
Length
;
return
new
StringBuilder
(
length
)
.
Append
(
scheme
)
.
Append
(
SchemeDelimiter
)
.
Append
(
encodedHost
)
.
Append
(
combinedPath
)
.
Append
(
encodedQuery
)
.
Append
(
encodedFragment
)
.
ToString
();
}
/// <summary>
/// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers
/// and other HTTP operations.
/// </summary>
/// <param name="request">The request to assemble the uri pieces from.</param>
/// <returns></returns>
public
string
GetEncodedUrl
(
HttpRequest
request
)
{
return
BuildAbsolute
(
request
.
Scheme
,
request
.
Host
,
request
.
PathBase
,
request
.
Path
,
request
.
QueryString
);
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/Dashboard/GatewayProxy/IRequestMapper.cs
0 → 100644
View file @
89e599a3
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
{
using
System.Net.Http
;
using
System.Threading.Tasks
;
using
Microsoft.AspNetCore.Http
;
public
interface
IRequestMapper
{
Task
<
HttpRequestMessage
>
Map
(
HttpRequest
request
);
}
}
\ No newline at end of file
src/DotNetCore.CAP/Dashboard/GatewayProxy/IRequestScopedDataRepository.HttpData.cs
0 → 100644
View file @
89e599a3
using
System
;
using
System.Collections.Concurrent
;
using
Microsoft.AspNetCore.Http
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
{
public
class
HttpDataRepository
:
IRequestScopedDataRepository
{
private
readonly
IHttpContextAccessor
_httpContextAccessor
;
public
HttpDataRepository
(
IHttpContextAccessor
httpContextAccessor
)
{
_httpContextAccessor
=
httpContextAccessor
;
}
public
void
Add
<
T
>(
string
key
,
T
value
)
{
_httpContextAccessor
.
HttpContext
.
Items
.
Add
(
key
,
value
);
}
public
T
Get
<
T
>(
string
key
)
{
object
obj
;
if
(
_httpContextAccessor
.
HttpContext
.
Items
.
TryGetValue
(
key
,
out
obj
))
{
return
(
T
)
obj
;
}
throw
new
Exception
(
$"Unable to find data for key:
{
key
}
"
);
}
}
public
class
ScopedDataRepository
:
IRequestScopedDataRepository
{
private
readonly
ConcurrentDictionary
<
string
,
object
>
dictionary
=
null
;
public
ScopedDataRepository
()
{
dictionary
=
new
ConcurrentDictionary
<
string
,
object
>();
}
public
void
Add
<
T
>(
string
key
,
T
value
)
{
dictionary
.
AddOrUpdate
(
key
,
value
,
(
k
,
v
)
=>
value
);
}
public
T
Get
<
T
>(
string
key
)
{
if
(
dictionary
.
TryGetValue
(
key
,
out
object
t
))
{
return
(
T
)
t
;
}
throw
new
Exception
(
$"Unable to find data for key:
{
key
}
"
);
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/Dashboard/GatewayProxy/IRequestScopedDataRepository.cs
View file @
89e599a3
using
System.Collections.Generic
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
namespace
DotNetCore.CAP.Dashboard.GatewayProxy
{
{
public
interface
IRequestScopedDataRepository
public
interface
IRequestScopedDataRepository
{
{
void
Add
<
T
>(
string
key
,
T
value
);
void
Add
<
T
>(
string
key
,
T
value
);
T
Get
<
T
>(
string
key
);
T
Get
<
T
>(
string
key
);
}
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/Dashboard/GatewayProxy/Requester/HttpClientBuilder.cs
View file @
89e599a3
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Linq
;
using
System.Net
;
using
System.Net.Http
;
using
System.Net.Http
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
...
@@ -22,7 +21,6 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy.Requester
...
@@ -22,7 +21,6 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy.Requester
private
HttpMessageHandler
CreateHttpMessageHandler
(
HttpMessageHandler
httpMessageHandler
)
private
HttpMessageHandler
CreateHttpMessageHandler
(
HttpMessageHandler
httpMessageHandler
)
{
{
_handlers
_handlers
.
OrderByDescending
(
handler
=>
handler
.
Key
)
.
OrderByDescending
(
handler
=>
handler
.
Key
)
.
Select
(
handler
=>
handler
.
Value
)
.
Select
(
handler
=>
handler
.
Value
)
...
...
src/DotNetCore.CAP/Dashboard/GatewayProxy/Requester/HttpClientHttpRequester.cs
View file @
89e599a3
using
System
;
using
System
;
using
System.Collections.Concurrent
;
using
System.Net.Http
;
using
System.Net.Http
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Microsoft.Extensions.Logging
;
using
Microsoft.Extensions.Logging
;
...
@@ -38,7 +37,6 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy.Requester
...
@@ -38,7 +37,6 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy.Requester
{
{
_cacheHandlers
.
Set
(
cacheKey
,
httpClient
,
TimeSpan
.
FromHours
(
24
));
_cacheHandlers
.
Set
(
cacheKey
,
httpClient
,
TimeSpan
.
FromHours
(
24
));
}
}
}
}
private
IHttpClient
GetHttpClient
(
string
cacheKey
,
IHttpClientBuilder
builder
)
private
IHttpClient
GetHttpClient
(
string
cacheKey
,
IHttpClientBuilder
builder
)
...
...
src/DotNetCore.CAP/Dashboard/GatewayProxy/Requester/IHttpClient.cs
View file @
89e599a3
using
System
;
using
System.Net.Http
;
using
System.Net.Http
;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
namespace
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
...
...
src/DotNetCore.CAP/Dashboard/GatewayProxy/Requester/IHttpClientBuilder.cs
View file @
89e599a3
using
System
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Net.Http
;
using
System.Threading.Tasks
;
using
System.Net
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
{
{
public
interface
IHttpClientBuilder
public
interface
IHttpClientBuilder
{
{
...
...
src/DotNetCore.CAP/Dashboard/GatewayProxy/Requester/IHttpClientCache.cs
View file @
89e599a3
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Net.Http
;
using
System.Threading.Tasks
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
namespace
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
{
{
public
interface
IHttpClientCache
public
interface
IHttpClientCache
{
{
bool
Exists
(
string
id
);
bool
Exists
(
string
id
);
IHttpClient
Get
(
string
id
);
IHttpClient
Get
(
string
id
);
void
Remove
(
string
id
);
void
Remove
(
string
id
);
void
Set
(
string
id
,
IHttpClient
handler
,
TimeSpan
expirationTime
);
void
Set
(
string
id
,
IHttpClient
handler
,
TimeSpan
expirationTime
);
}
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/Dashboard/GatewayProxy/Requester/IHttpRequester.cs
View file @
89e599a3
src/DotNetCore.CAP/Dashboard/GatewayProxy/Requester/MemoryHttpClientCache.cs
View file @
89e599a3
using
System
;
using
System
;
using
System.Collections.Concurrent
;
using
System.Collections.Concurrent
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Net.Http
;
using
System.Threading.Tasks
;
namespace
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
namespace
DotNetCore.CAP.Dashboard.GatewayProxy.Requester
{
{
...
@@ -34,7 +30,7 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy.Requester
...
@@ -34,7 +30,7 @@ namespace DotNetCore.CAP.Dashboard.GatewayProxy.Requester
public
IHttpClient
Get
(
string
id
)
public
IHttpClient
Get
(
string
id
)
{
{
IHttpClient
client
=
null
;
IHttpClient
client
=
null
;
ConcurrentQueue
<
IHttpClient
>
connectionQueue
;
ConcurrentQueue
<
IHttpClient
>
connectionQueue
;
if
(
_httpClientsCache
.
TryGetValue
(
id
,
out
connectionQueue
))
if
(
_httpClientsCache
.
TryGetValue
(
id
,
out
connectionQueue
))
{
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment