Recursive decoding of URI component in python like javascript - javascript

I have a encoded URI component "http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2". I could able to convert this to "http://www.yelp.com/biz/carriage-house-café-houston-2" by applying decodeURIComponent function recursively as below
function recursiveDecodeURIComponent(uriComponent){
try{
var decodedURIComponent = decodeURIComponent(uriComponent);
if(decodedURIComponent == uriComponent){
return decodedURIComponent;
}
return recursiveDecodeURIComponent(decodedURIComponent);
}catch(e){
return uriComponent;
}
}
console.log(recursiveDecodeURIComponent("http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2"))
Outputs: "http://www.yelp.com/biz/carriage-house-café-houston-2".
I would like to get the same in python.
I tried the following:
print urllib2.unquote(urllib2.unquote(urllib2.unquote("http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2").decode("utf-8")))
but I got http://www.yelp.com/biz/carriage-house-café-houston-2. Instead of Expected character é, I got 'é' irrespective of any number of calling urllib2.unquote.
I am using python2.7.3, can anyone help me?

I guess a simple loop should do the trick:
uri = "http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2"
while True:
dec = urllib2.unquote(uri)
if dec == uri:
break
uri = dec
uri = uri.decode('utf8')
print '%r' % uri
# u'http://www.yelp.com/biz/carriage-house-caf\xe9-houston-2'

Related

Working with memory to fetch string yields incorrect result

I am following the solutions from here:
How can I return a JavaScript string from a WebAssembly function
and here:
How to return a string (or similar) from Rust in WebAssembly?
However, when reading from memory I am not getting the desired results.
AssemblyScript file, helloWorldModule.ts:
export function getMessageLocation(): string {
return "Hello World";
}
index.html:
<script>
fetch("helloWorldModule.wasm").then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, {imports: {}})
).then(results => {
var linearMemory = results.instance.exports.memory;
var offset = results.instance.exports.getMessageLocation();
var stringBuffer = new Uint8Array(linearMemory.buffer, offset, 11);
let str = '';
for (let i=0; i<stringBuffer.length; i++) {
str += String.fromCharCode(stringBuffer[i]);
}
debugger;
});
</script>
This returns an offset of 32. And finally yields a string that starts too early and has spaces between each letter of "Hello World":
However, if I change the array to an Int16Array, and add 8 to the offset (which was 32), to make an offset of 40. Like so:
<script>
fetch("helloWorldModule.wasm").then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, {imports: {}})
).then(results => {
var linearMemory = results.instance.exports.memory;
var offset = results.instance.exports.getMessageLocation();
var stringBuffer = new Int16Array(linearMemory.buffer, offset+8, 11);
let str = '';
for (let i=0; i<stringBuffer.length; i++) {
str += String.fromCharCode(stringBuffer[i]);
}
debugger;
});
</script>
Then we get the correct result:
Why does the first set of code not work like its supposed to in the links I provided? Why do I need to change it to work with Int16Array to get rid of the space between "H" and "e" for example? Why do I need to add 8 bytes to the offset?
In summary, what on earth is going on here?
Edit: Another clue, is if I use a TextDecoder on the UInt8 array, decoding as UTF-16 looks more correct than decoding as UTF-8:
AssemblyScript uses utf-16: https://github.com/AssemblyScript/assemblyscript/issues/43
Additionally AssemblyScript stores the length of the string in the first 32 or 64 bits.
That's why my code behaves differently. The examples in the links at the top of this post were for C++ and Rust, which do string encoding differently

Is there Any Way to Convert Mongo ObjectId into string by javascript/jquery

Is it possible to convert mongo objectId into string.
The above pictures shows data i received and shown in console.I need id value in string form .but ObjectId is returning as object
In Database id is look like this- 565d3bf4cefddf1748d1fc5e -objectId and i need id exactly like this –
According to the Mongo documentation:
a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.
You can check it out here: https://docs.mongodb.org/manual/reference/object-id/
So in javascript you could do something like this.
var mId = {
Timestamp:1448950573,
Machine:13565407,
Pid:1756,
Increment:8888962
};
function getId(mongoId) {
var result =
pad0(mongoId.Timestamp.toString(16), 8) +
pad0(mongoId.Machine.toString(16), 6) +
pad0(mongoId.Pid.toString(16), 4) +
pad0(mongoId.Increment.toString(16), 6);
return result;
}
function pad0(str, len) {
var zeros = "00000000000000000000000000";
if (str.length < len) {
return zeros.substr(0, len-str.length) + str;
}
return str;
}
console.log(getId(mId))
It produces "565d3b2dcefddf06dc87a282" which was not exactly the id you had, but that might just be a tweak or i was working with different data :D.
EDIT
Added a padding function so that zeros are not truncated.
Hope that helps
EDIT:
I assume you are using c# to connect to and serve documents from the mongo DB. In that case, there is a driver that also supports toString().
Here is an example using the mongo csharp driver:
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
// ...
string outputFileName; // initialize to the output file
IMongoCollection<BsonDocument> collection; // initialize to the collection to read from
using (var streamWriter = new StreamWriter(outputFileName))
{
await collection.Find(new BsonDocument())
.ForEachAsync(async (document) =>
{
using (var stringWriter = new StringWriter())
using (var jsonWriter = new JsonWriter(stringWriter))
{
var context = BsonSerializationContext.CreateRoot(jsonWriter);
collection.DocumentSerializer.Serialize(context, document);
var line = stringWriter.ToString();
await streamWriter.WriteLineAsync(line);
}
});
}
ORIGINAL:
These are Mongo ObjectId's and if you haven't already deserialised the document they should support a toString method that will return a hexadecimal string.
but if you want this applied to the whole document, using JSON.stringify(MogoDocument) should deserialize this for you into a plain object.

Check file extension with regex - application/vnd.ms-excel

I need to check if a file is a xls. I am trying this code, but I upload a xls file and I am getting the error Invalid file.
var acceptFileTypes = /(.|\/)(application\/vnd.ms-excel)$/i;
console.log(data.originalFiles[0]['type']); //application/vnd.ms-excel
if (!acceptFileTypes.test(data.originalFiles[0]['type'])) {
uploadErrors.push('Invalid file');
}
There is something wrong with the regex?
Here's your error: (.|\/)
This part needs to consume one character - it is actually equivalent to ..
Remove that part and you'll be fine. You also need to escape the periods:
var acceptFileTypes = /application\/vnd\.ms-excel$/i;
Of course, this is equivalent to:
var acceptFileTypes = "application/vnd.ms-excel";
var isExcel = yourFile.toLowerCase().indexOf(acceptFileTypes, yourFile.length - acceptFileTypes.length) == 0;

Uncaught Error: INVALID_CHARACTER_ERR: DOM Exception 5

$('form#update').submit(function(){
var _data = $(this).serializeArray();
var param = {};
$.map(_data,function(a,b){
if(a.name=='HotelName'){
param[a.name] = window.btoa(a.value);
}
else{
param[a.name] = a.value;
}
});
console.log(param);
$.post('api.coupon_edit_post.php',param,function(r){
coupons();
reloadMarkers();
});
return false;
});
Is window.btoa causing this error?
I'm using window.btoa so I can pass most of the characters.
EDIT: I tried the MDN solution but it's not working. I am using Google Chrome Version 24.0.1312.57 m
The Error comes definitely from window.btoa.
You should modify the MDN solution and omit the decoding step before the conversion, like following:
function utf8_to_b64( str ) {
return window.btoa(encodeURIComponent( str ));
}
function b64_to_utf8( str ) {
return decodeURIComponent(window.atob( str ));
}
This creates the b64 string from the encoded string instead of decoding it again (which again would create symbols, btoa can't process). Now this works:
utf8_to_b64('✓ à la mode');
b64_to_utf8("JUUyJTlDJTkzJTIwJUMzJUEwJTIwbGElMjBtb2Rl");
of course due to the encoding this significantly lengthens the b64 string.
You can now modify your example:
param[a.name] = utf8_to_b64(a.value);

OAuth nonce value

I am working with the FatSecret REST API
Im using the OAuthSimple javascript library to generate the signed url.
Here's the code I have -
params['oauth_timestamp'] = Math.floor(new Date().getTime()/1000);
params['oauth_nonce'] = '1234';
params['oauth_version'] = '1.0';
var paramStr = '';
for(var key in params){
paramStr += key+"="+params[key]+"&";
}
paramStr = paramStr.substring(0,paramStr.length-1);
var oauth = OAuthSimple();
oauth.setAction('GET');
var o = oauth.sign(
{
path:this.requestURL,
parameters: paramStr,
signatures:{
api_key:this.apiKey,
shared_secret:this.sharedSecret,
access_token: this.accessToken,
access_secret: this.accessSecret
}
});
console.log(o.signed_url);
return o.signed_url;
params is an associative array containing all the non oauth related parameters for this call.
When I use this signed url I get an "invalid/used nonce"
The OAuth Testing Tool uses the same OAuthSimple library and if I put in all the same parameters (including the timestamp) it generates exactly the same url.
The only difference is that the url generated by the testing tool works and gives me the full response from the server. The url generated by my code does't.
I tried various nonce values including sending a MD5 of the timestamp but I get the same error. The reason I'm using 1234 right now is that the testing tool uses 1234 by default and that seems to work.
Any help is appreciated. Thanks in advance.
Updating #Saravanan's answer with something that works on current browsers:
function genNonce() {
const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._~'
const result = [];
window.crypto.getRandomValues(new Uint8Array(32)).forEach(c =>
result.push(charset[c % charset.length]));
return result.join('');
}
console.info(genNonce());
The nonce value as per twitter documentation:
The value for this request was generated by base64 encoding 32 bytes of random data, and stripping out all non-word characters, but any
approach which produces a relatively random alphanumeric string should
be OK here.
Based on the above notes, I use the following javascript code to generate nonce value each time I send a request:
var nonceLen = 32;
return crypto.randomBytes(Math.ceil(nonceLen * 3 / 4))
.toString('base64') // convert to base64 format
.slice(0, nonceLen) // return required number of characters
.replace(/\+/g, '0') // replace '+' with '0'
.replace(/\//g, '0'); // replace '/' with '0'
Try this if it works!
Try this
This works every time
var nonce = Math.random().toString(36).replace(/[^a-z]/, '').substr(2);

Categories