Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward slash character ends variable in python/html

I am trying to pass a URL to a variable in python (youtube url of the video to be played on the Raspberry Pi), but somewhere along the way the forward slash character is being interpreted as an end of the string/variable. So instead of getting "http://www.youtube.com/watch?v=5NV6Rdv1a3I", I get "http:".

I am using the WebIOPi server to display a webpage in html that contains a textarea. When I click a button on the webpage, the function sendLink() is called and the text from the textarea passed as an argument.

Content of index.html:

    function sendLink() {
            var text = $('textarea#videolink').text();
            webiopi().callMacro("playVideo", text);
    }

    ...

    <textarea rows="1" cols="30" id="videolink">Enter YouTube link here</textarea>

The function callMacro calls a macro named playVideo, written in another script in python:

@webiopi.macro
def playVideo(text):
    print (text)
    webiopi.debug(text)

When I enter "a/b/c" into the textarea and click the button, only "a" is displayed by print and webiopi.debug, even though the general debug information that is displayed along with it says "POST /macros/playVideo/a/b/c HTTP/1.1" 200, which I believe means that the variable is being passed to the function correctly.

(Idea for sending text entered into the textarea taken form here: http://timcorrigan.com/raspberry-pi-tracked-robot-streaming-video-and-text-to-speech/)

How do I solve this? Any solution is appreciated.

like image 310
Marvos Avatar asked Aug 25 '26 17:08

Marvos


1 Answers

Relevant source is do_POST on line 180 here.

Specifically notice:

paths = relativePath.split("/")
if len(paths) > 2:
    value = paths[2]

Where value is eventually passed to your macro. It shows that anything past the second / is discarded. As an aside, it appears you can comma separate arguments in the url and they will be passed positionally.

Your best option is probably to escape the string in javascript before sending it and unescape it in your macro. Looks like you will need to escape all url unsafe characters, /, and ,.

like image 154
kalhartt Avatar answered Aug 28 '26 05:08

kalhartt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!