Commits
Tim Donohue authored and GitHub committed 6610ccc8c1f Merge
20 20 | constructor(@Inject(REQUEST) protected req: any) { |
21 21 | } |
22 22 | |
23 23 | /** |
24 24 | * Intercept http requests and add the client's IP to the X-Forwarded-For header |
25 25 | * @param httpRequest |
26 26 | * @param next |
27 27 | */ |
28 28 | intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
29 29 | const clientIp = this.req.get('x-forwarded-for') || this.req.connection.remoteAddress; |
30 - | return next.handle(httpRequest.clone({ setHeaders: { 'X-Forwarded-For': clientIp } })); |
30 + | const headers = { 'X-Forwarded-For': clientIp }; |
31 + | |
32 + | // if the request has a user-agent retain it |
33 + | const userAgent = this.req.get('user-agent'); |
34 + | if (userAgent) { |
35 + | headers['User-Agent'] = userAgent; |
36 + | } |
37 + | |
38 + | return next.handle(httpRequest.clone({ setHeaders: headers })); |
31 39 | } |
32 40 | } |