FetchURLHash

From FreeSWITCH Wiki
Jump to: navigation, search

Contents

Description

Fetch the response from a webserver and store it in a hash. The webserver must return data with

Content-type: text/plain\n\n

header and attribute=value pairs in body eg:
moo=ojnk
foo=bar
spaces on either side of the name or value will be trimmed off, so 'foo = bar' is valid and in Javascript would be the same as 'foo=bar'

You may have to escape any " with \" to get it to work properly due to the way this is converted into Javascript. eg

foo=\"bar\"

Synopsis

fetchURLHash(url,hash)
  • url - any CURL supported format (you can encode data you want to send to the server)
  • hash - name of the hash to save data into

Note: the hash variable name must be between apostrophes.

Example

var myhash = new Array(); // must be defined in the same scope, ie same function
fetchURLHash("http://www.somewhere.tld/somepage.html","myhash");
for (key in myhash) {
    console_log(key + ": " + myhash[key] + "\n");
}

Security Warning

The data returned is processed with eval(), which means that if someone has control to send anything they want it will be processed and they can overwrite other variables, execute arbitrary code, whatever. An example might be:

foo=bar";console_log("pwned\n");var blah="

the last var blah=" is to catch the trailing quote and not have a parse error.

Do not fetch arbitrary URLs off the internet with this, only use sites that you maintain control of to avoid an evil user from doing this.

See Also