Why does jsdom set my attribute *values* to all lowercase? - jsdom

here is sample code. running in webstorm.
"jsdom": "^13.2.0"
node 10.15.0
const jsdom = require("jsdom");
const {JSDOM} = jsdom;
dom = new JSDOM("");
uri = "file://testResource/test.js"
const script = dom.window.document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.setAttribute("src",uri);
console.log(`loading script ${script.src}`); //loading script file://testresource/test.js
console.log(`loading script ${uri}`); //loading script file://testResource/test.js
Note the case change on the word Resource.
I understand that the attribute names are not case sensitive, but the values should be, no?

The way your URI is written the testResource part is a hostname. Host names are case-insensitive and URI Generic Syntax (RFC3986) states:
Although host is case-insensitive, producers and normalizers should use lowercase for registered names and hexadecimal addresses for the sake of uniformity, while only using uppercase letters for percent-encodings.
What is going on here is that JSDOM is normalizing the hostname, and in doing so is making it lowercase. (BTW, type the same URI in Chrome and you'll see Chrome does the same.)
If your URI was meant to not have a hostname, then you need to write it like this:
file:///testResource/test.js
Note the additional slash before testResource. This URI has an empty hostname. When the hostname is empty, localhost is the understood host.

Related

A good name for a URL path and query string and fragment?

URLs can be broken up into these components:
Sample URL: http://www.server.com:8080/path?query=string#fragment
protocol = http
host = www.server.com
port = 8080
path = /path
query = ?query=string
fragment = #fragment
Is there an established name for everything that comes after port (path, query, and fragment)? I was tempted to just call this "path" but that is not a good name as it does not include the query and fragment.
The IETF URI Spec defines the combination of these elements (path, query, and fragment) as a Relative Reference.
Perhaps "relative URL", as in "relative to Site Root"
The URL structure you are giving is containing the basic elements, and the fragment is usually coming before the query, the query in general is used when you are listing a part of data, so you need to access to it by this query, and parameters if needed.
Here a good review to list the steps for creating a good url.
there is no established name for everything together due to them all being different. Also you forgot the parameters part of the url. The parameters go in the query.

Apigee remove URL string before forwarding request to target

I want to transform a URL before it is redirected to the target, removing a string. I have the following flow in Apigee:
USER -> APIGEE -> APPLICATION -> APIGEE -> USER
The user requests and then its URL should be rewritten removing bar from the URL.
BEFORE apigee.url.com/foo/bar/pBxATaojIn8tk5dvQdNJ
AFTER target.url.com/foo/pBxATaojIn8tk5dvQdNJ
I use Proxy Endpoints and Target Endpoints and try to rewrite using a PreFlow hook with Javascript in the Target Endpoint, without success rewriting the proxy.pathsuffix. How can I solve this?
I now use the following solution:
// Disable copy path
context.setVariable("target.copy.pathsuffix", false);
// Replace string in incoming proxy URL path
var proxyPathSuffix = context.getVariable("proxy.pathsuffix");
var fooBarAfter = proxyPathSuffix.replace('/fooToReplace', '');
// Fetch target outgoing url path
var targetBasePath = context.getVariable("target.basepath");
var urlPath = targetBasePath.concat(fooBarAfter);
// Replace outgoing url
var targetUrl = context.getVariable("target.url");
targetUrl = targetUrl.replace(targetBasePath, urlPath);
context.setVariable("target.url", targetUrl);
I came up with it looking at the available variables here. As this is JS, if someone comes up with a better solution I would be happy!
From my understanding proxy.pathsuffix is a read only variable so you can't override it.
Assign your target endpoint to target.url instead. (I usually use in Assign Message Policy of the request)
Example:
<AssignVariable>
<Name>target.url</Name>
<Ref>yourTargetUrlValue</Ref>
</AssignVariable>
In my Assign Message Policy of the request, I have added the block of code above for defined my target url.
You can assign directly like <Ref>http://test.com</Ref> or assign via variable like <Ref>{targetUrlVal}</Ref>
If you got the url from somewhere, don't forget to assign value to your variable before use it by using context.setVariable("targetUrlVal", "http://test-01.com");
Hopefully my answer will help you.
I solved this similar to how you did it. See my answer on another SO question that answers this.

Nginx lua modify html response after proxy_pass

What I'm trying to do for a PoC and is to add a href to web pages coming from a dynamic backend server. Adding the href is easy using "subs_filter", but I need to use information embedded within the response to construct the href.
Is it possible to use LUA to process the response from proxy_pass, modify it and return to requester (client)?
Any and all suggestions welcome.
Below is the code I'm looking at, now I understand Lua better and how nginx uses it I see that 'body_filter' is the correct way to. However the code seems simple enough but i can't get the regex to work.
Further background, I'm trying to parse the returned proxy_pass response, parse it for a start and end time, then construct a JS script url placed into the head.
Example response that I want to regex against.
Informações Adicionais
Horário de início: 08H50
Horário de término: 09H14
The code from within the 'location {}'
body_filter_by_lua '
-- my regex which is validate but doesn't seem to be within LUAJIT
--local from, to, err = ngx.re.find(ngx.arg[1], "(.início: *\d{2}H\d{2})", "jo")
local from, to, err = ngx.re.find(ngx.arg[1], "início", "jo")
replacestr = string.sub(ngx.arg[1], to, 5)
replaceme = "<script></script></head>"
ngx.arg[1] = ngx.re.sub(ngx.arg[1],"</head>", replaceme)
';
Changing "início" to "head" for example works, so I'm assuming it is the accented char but I'm unable to find confirmation of this.
Changing "início" to "\d{2}H\d{2}" fails, with "body_filter_by_lua:5: invalid escape sequence near '"'"
I discovered what I mentioned in the comments regarding 'nix.header.content_length' and importantly nginx and lua require double escaping see: lua-nginx-module special pcre sequences for more details.
The accented chars needed the flag 'u' adding to 'jo' of the 'ngx.re.find'
user "body_filter_by_lua_file"
Equivalent to body_filter_by_lua, except that the file specified by contains the Lua code, or, as from the v0.5.0rc32 release, the Lua/LuaJIT bytecode to be executed.
When a relative path like foo/bar.lua is given, they will be turned into the absolute path relative to the server prefix path determined by the -p PATH command-line option while starting the Nginx server.
This directive was first introduced in the v0.5.0rc32 release.

Query string after the domain name

I am trying to add a query string at the end of URL for a hyperlink control as follows
HyperLink testLink = new HyperLink();
testLink.NavigateUrl = "http://www.example.com" + "?siteId=asd343s32kj343dce";
But when this is rendered in the browser it is displaying as
http://www.example.com/?siteId=asd343s32kj343dce (/ char after the .com).
And if the testLink.NavigateUrl = "http://www.example.com/abc.aspx" + "?siteId=asd343s32kj343dce";
Then the link is rendered correctly as http://www.abcd.com/abc.aspx?siteId=asd343s32kj343dce (No extra characters).
Am I missing any thing? Please advice.
Thank you,
Krishna.
The browser is correcting the URL for you by assuming that there should be a slash after the domain name. You might run into problems with browsers that doesn't do this, so you should correct the URL to:
testLink.NavigateUrl = "http://www.abcd.com/" + "?siteId=asd343s32kj343dce";
The reason that the slash should be after the domain name is that the domain name itself can not be a resource. The domain name just specifies the web site, the URL has to have something that specifies a resource on that site, and the slash specifies the default page in the root folder of the site.
this is normal, the / tell that the domain name ended and you are now inside the structure of the website (root context in this case).
the second one is normal because abc.aspx is a webpage and it can accept querystring. a domain cannot accept a querystring.
An HTTP URL takes the form:
http://<host>:<port>/<path>?<searchpart>
where <host> and <port> are as described in Section 3.1. If :<port>
is omitted, the port defaults to 80. No user name or password is
allowed. <path> is an HTTP selector, and <searchpart> is a query
string. The <path> is optional, as is the <searchpart> and its
preceding "?". If neither <path> nor <searchpart> is present, the "/"
may also be omitted.
https://www.rfc-editor.org/rfc/rfc1738#section-3.3
Although http://example.com?query is a valid URI. The normalization of HTTP URIs states that http://example.com?query and http://example.com/?query are equal:
[…] because the "http" scheme makes use of an authority component, has a default port of "80", and defines an empty path to be equivalent to "/", the following four URIs are equivalent:
http://example.com
http://example.com/
http://example.com:/
http://example.com:80/
In general, a URI that uses the generic syntax for authority with an empty path should be normalized to a path of "/".

Setting HttpContext.Current.Request.Url.Host

I need to replace the host part of the Uri
HttpContext.Current.Request.Url.Host = "newDomain";
you can't set host. Is there a quick and easy way to do this to a Uri for reusing it to redirect somewhere else?
Use the UriBuilder class to change URIs, e.g.
var original = HttpContext.Current.Request.Url;
var changed = (new UriBuilder(original) { Host = "newDomain" }).Uri;
URIs are tricky little beasts with plenty of semantics you might not know or expect, so don't go using string functions on them unless you absolutely have to.

Resources