UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

474 lines (473 loc) 18 kB
<!doctype html> <html lang="en"> <head> <title>Code coverage report for src/middleware/rewriteUrls.coffee</title> <meta charset="utf-8" /> <link rel="stylesheet" href="../../prettify.css" /> <link rel="stylesheet" href="../../base.css" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type='text/css'> .coverage-summary .sorter { background-image: url(../../sort-arrow-sprite.png); } </style> </head> <body> <div class='wrapper'> <div class='pad1'> <h1> <a href="../../index.html">All files</a> / <a href="index.html">src/middleware</a> rewriteUrls.coffee </h1> <div class='clearfix'> <div class='fl pad1y space-right2'> <span class="strong">28% </span> <span class="quiet">Statements</span> <span class='fraction'>14/50</span> </div> <div class='fl pad1y space-right2'> <span class="strong">0% </span> <span class="quiet">Branches</span> <span class='fraction'>0/8</span> </div> <div class='fl pad1y space-right2'> <span class="strong">0% </span> <span class="quiet">Functions</span> <span class='fraction'>0/8</span> </div> <div class='fl pad1y space-right2'> <span class="strong">28.57% </span> <span class="quiet">Lines</span> <span class='fraction'>14/49</span> </div> </div> </div> <div class='status-line low'></div> <pre><table class="coverage"> <tr><td class="line-count quiet">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137</td><td class="line-coverage quiet"><span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1x</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">url = require 'url' winston = require 'winston' Q = require 'q' &nbsp; Channel = (require '../../lib/model/channels').Channel config = require '../config/config' routerConf = config.get 'router' utils = require '../../lib/utils' router = require '../../lib/middleware/router' &nbsp; invertPathTransform = <span class="fstat-no" title="function not covered" >(</span>pathTransform) -&gt; # see https://regex101.com/r/lW0cN0/1 for an explanation of this regex <span class="cstat-no" title="statement not covered" > return p</span>athTransform.replace /s\/(.*?)\/(.*?)(?:$|\/(.*)$)/, 's/$2/$1/$3' &nbsp; exports.fetchRewriteConfig = <span class="fstat-no" title="function not covered" >(</span>channel, authType, callback) -&gt; # set the user defined rewrite config from current channel <span class="cstat-no" title="statement not covered" > rwConfig = [</span>] if channel.rewriteUrlsConfig? <span class="cstat-no" title="statement not covered" > rwConfig = rwConfig.concat channel.rewriteUrlsConfig</span> &nbsp; if channel.addAutoRewriteRules ### # Add in default (virtual) rewrite rules for hosts we proxy # # For example if the SHR for some reason sent back a link to a patient in the CR # (using a direct link to the CR), then if we have a channel that points to the # CR on a primary route we are able to rewrite the link to point to us instead # because we know that host. ### utils.getAllChannelsInPriorityOrder <span class="fstat-no" title="function not covered" >(</span>err, channels) -&gt; if err? <span class="cstat-no" title="statement not covered" > return callback err</span> &nbsp; <span class="cstat-no" title="statement not covered" > for channel in channels</span> <span class="cstat-no" title="statement not covered" > for route in channel.routes</span> do <span class="fstat-no" title="function not covered" ></span>-&gt; if route.primary ### # if this channel has a pathTranform on its primary route then # invert the path transform so that links that point to this route # have the path transform reversed when they are rewritten # # For example, say we have a channel with urlPattern=/CSD/ and a # pathTransform on the primary route as follows pathTransform=s/CSD/ihris/ # (ie. the actual server we are proxying is running on http://&lt;host&gt;:&lt;port&gt;/ihirs/). # If we get links back from this server it will be something like # http://&lt;host&gt;:&lt;port&gt;/ihirs/something/123 but we need it to be # http://&lt;him_host&gt;:&lt;him_port&gt;/CSD/something/123. To do this we can reverse # the pathTransform on the route (s/ihris/CSD/) and apply it while doing the # rewrite. ### if route.pathTransform <span class="cstat-no" title="statement not covered" > inverseTransform = invertPathTransform route.pathTransform</span> &nbsp; # rewrite to the secure port if tls was used for this transaction if authType? is 'tls' <span class="cstat-no" title="statement not covered" > toPort = routerConf.httpsPort</span> else <span class="cstat-no" title="statement not covered" > toPort = routerConf.httpPort</span> &nbsp; # add 'virtual' rewrite config after any user defined config that has been set rwConfig.push 'fromHost': route.host 'toHost': routerConf.externalHostname 'fromPort': route.port 'toPort': toPort 'pathTransform': if inverseTransform then inverseTransform else null &nbsp; callback null, rwConfig else callback null, rwConfig &nbsp; rewriteUrls = <span class="fstat-no" title="function not covered" >(</span>body, channel, authType, callback) -&gt; exports.fetchRewriteConfig channel, authType, <span class="fstat-no" title="function not covered" >(</span>err, rwConfig) -&gt; if err? <span class="cstat-no" title="statement not covered" > return callback err</span> &nbsp; # rewrite each found href, src or fullUrl attribute (in JSON or XML) # See https://regex101.com/r/uY3fO1/1 for an explanation of this regex <span class="cstat-no" title="statement not covered" > newBody = b</span>ody.replace /["|']?(?:href|src|fullUrl)["|']?[:|=]\s?["|'](\S*?)["|']/g, <span class="fstat-no" title="function not covered" >(</span>match, hrefUrl) -&gt; <span class="cstat-no" title="statement not covered" > hrefUrlObj = u</span>rl.parse hrefUrl &nbsp; # default to using this channel's host if no host so we can match a rewrite rule if not hrefUrlObj.host? <span class="cstat-no" title="statement not covered" ><span class="cstat-no" title="statement not covered" > for route in channel.routes</span></span> if route.primary <span class="cstat-no" title="statement not covered" > hrefUrlObj.hostname = route.h</span>ost <span class="cstat-no" title="statement not covered" > hrefUrlObj.port = r</span>oute.port.toString() <span class="cstat-no" title="statement not covered" > relativePath = t</span>rue <span class="cstat-no" title="statement not covered" > break</span> &nbsp; <span class="cstat-no" title="statement not covered" > for rewriteRule in rwConfig</span> # if we find a matching rewrite rule if rewriteRule.fromHost.toLowerCase() is hrefUrlObj.hostname and (rewriteRule.fromPort.toString() is hrefUrlObj.port or (rewriteRule.fromPort is 80 and hrefUrlObj.port is null)) <span class="cstat-no" title="statement not covered" > hrefUrlObj.host = n</span>ull # so that hostname and port are used separately <span class="cstat-no" title="statement not covered" > hrefUrlObj.hostname = rewriteRule.t</span>oHost <span class="cstat-no" title="statement not covered" > hrefUrlObj.port = rewriteRule.t</span>oPort &nbsp; # rewrite protocol depending on the port the rewriteRule uses if hrefUrlObj.protocol <span class="cstat-no" title="statement not covered" > if rewriteRule.toPort is routerConf.httpsPort</span> <span class="cstat-no" title="statement not covered" > hrefUrlObj.protocol = 'https'</span> else <span class="cstat-no" title="statement not covered" > hrefUrlObj.protocol = 'http'</span> &nbsp; # if this rewrite rule requires the path to be transformed then do the transform if rewriteRule.pathTransform <span class="cstat-no" title="statement not covered" > hrefUrlObj.pathname = router.transformPath hrefUrlObj.pathname, rewriteRule.pathTransform</span> &nbsp; # we only run the first matching rule found <span class="cstat-no" title="statement not covered" > break</span> &nbsp; if relativePath # remove the host stuff before formating <span class="cstat-no" title="statement not covered" > hrefUrlObj.host = n</span>ull <span class="cstat-no" title="statement not covered" > hrefUrlObj.hostname = n</span>ull <span class="cstat-no" title="statement not covered" > hrefUrlObj.port = null</span> &nbsp; # replace the url in the match <span class="cstat-no" title="statement not covered" > replacement = u</span>rl.format hrefUrlObj <span class="cstat-no" title="statement not covered" > winston.debug "Rewriting url #{hrefUrl} as #{replacement}"</span> <span class="cstat-no" title="statement not covered" > return m</span>atch.replace hrefUrl, replacement &nbsp; callback null, newBody &nbsp; if process.env.NODE_ENV is 'test' exports.invertPathTransform = invertPathTransform exports.rewriteUrls = rewriteUrls &nbsp; exports.koaMiddleware = <span class="fstat-no" title="function not covered" >(</span>next) -&gt; # do nothing to the request <span class="cstat-no" title="statement not covered" > yield next</span> # on response rewrite urls if this.authorisedChannel.rewriteUrls <span class="cstat-no" title="statement not covered" > rewrite = Q</span>.denodeify rewriteUrls <span class="cstat-no" title="statement not covered" > this.response.body = y</span>ield rewrite this.response.body.toString(), this.authorisedChannel, this.authenticationType winston.info "Rewrote url in the response of transaction: #{this.transactionId}" &nbsp;</pre></td></tr> </table></pre> <div class='push'></div><!-- for sticky footer --> </div><!-- /wrapper --> <div class='footer quiet pad2 space-top1 center small'> Code coverage generated by <a href="http://istanbul-js.org/" target="_blank">istanbul</a> at Mon Oct 10 2016 13:39:22 GMT+0200 (SAST) </div> </div> <script src="../../prettify.js"></script> <script> window.onload = function () { if (typeof prettyPrint === 'function') { prettyPrint(); } }; </script> <script src="../../sorter.js"></script> </body> </html>