From 04d4d8a90bc98739667da60c5cfe126bb98dba5c Mon Sep 17 00:00:00 2001 From: Jeffrey Barron Date: Mon, 31 Aug 2020 17:06:20 -0500 Subject: [PATCH] Add ability to proxy requests (#34) * Add ability to proxy requests * Convert proxy settings to struct --- README.md | 2 ++ aws.cfc | 5 +++-- com/api.cfc | 5 +++-- com/http/coldfusion.cfc | 5 +++-- com/http/lucee.cfc | 6 ++++-- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0c76126..e152dec 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ aws = new path.to.awscfml.aws( ); ``` +*Note: An optional `httpProxy` argument is available when initializing the core `aws` component (a struct with `server` and `port` keys). When set this will proxy all AWS requests.* + ### ColdBox Module To use the library as a ColdBox Module, add the init arguments to the `moduleSettings` struct in `config/Coldbox.cfc`: diff --git a/aws.cfc b/aws.cfc index 8af80d3..d7a7bab 100644 --- a/aws.cfc +++ b/aws.cfc @@ -36,9 +36,10 @@ component { string awsKey = '', string awsSecretKey = '', string defaultRegion = '', - struct constructorArgs = { } + struct constructorArgs = { }, + struct httpProxy = { server: '', port: 80 } ) { - this.api = new com.api( awsKey, awsSecretKey, defaultRegion ); + this.api = new com.api( awsKey, awsSecretKey, defaultRegion, httpProxy ); for ( var service in variables.services ) { if ( structKeyExists( arguments.constructorArgs, service ) ) { diff --git a/com/api.cfc b/com/api.cfc index 15f6ef1..9130031 100644 --- a/com/api.cfc +++ b/com/api.cfc @@ -9,10 +9,11 @@ component accessors="true" { public any function init( required string awsKey, required string awsSecretKey, - required string defaultRegion + required string defaultRegion, + struct httpProxy = { server: '', port: 80 } ) { variables.utils = new utils(); - variables.httpService = server.keyExists( 'lucee' ) ? new http.lucee( utils ) : new http.coldfusion( utils ); + variables.httpService = server.keyExists( 'lucee' ) ? new http.lucee( utils, httpProxy ) : new http.coldfusion( utils, httpProxy ); variables.credentials = new credentials( awsKey, awsSecretKey, this ); variables.signer = new signature_v4( this ); variables.defaultRegion = arguments.defaultRegion.len() ? arguments.defaultRegion : utils.getSystemSetting( diff --git a/com/http/coldfusion.cfc b/com/http/coldfusion.cfc index 8a8bbb0..461978c 100644 --- a/com/http/coldfusion.cfc +++ b/com/http/coldfusion.cfc @@ -1,7 +1,8 @@ component { public any function init( - required any utils + required any utils, + struct httpProxy = { server: '', port: 80 } ) { variables.utils = utils; return this; @@ -21,7 +22,7 @@ component { var request_headers = utils.parseHeaders( headers ); var urlPath = 'http' & ( useSSL ? 's' : '' ) & '://' & fullPath; - cfhttp(url=urlPath, method=httpMethod, result="result", timeout=timeout) { + cfhttp(url=urlPath, method=httpMethod, result="result", timeout=timeout, proxyserver=httpProxy.server, proxyport=httpProxy.port) { for ( var header in request_headers ) { if ( header.name == 'host' ) continue; cfhttpparam(type="header", name=lCase( header.name ), value=header.value); diff --git a/com/http/lucee.cfc b/com/http/lucee.cfc index ef4406d..e5e4a4d 100644 --- a/com/http/lucee.cfc +++ b/com/http/lucee.cfc @@ -1,9 +1,11 @@ component { public any function init( - required any utils + required any utils, + struct httpProxy = { server: '', port: 80 } ) { variables.utils = utils; + variables.httpProxy = httpProxy; return this; } @@ -21,7 +23,7 @@ component { var request_headers = utils.parseHeaders( headers ); var urlPath = 'http' & ( useSSL ? 's' : '' ) & '://' & fullPath; - http url=urlPath method=httpMethod result="result" encodeurl=false timeout=timeout { + http url=urlPath method=httpMethod result="result" encodeurl=false timeout=timeout proxyServer=httpProxy.server proxyPort=httpProxy.port { for ( var header in request_headers ) { if ( header.name == 'host' ) continue; httpparam type="header" name=lCase( header.name ) value=header.value;