Facility Control

 View Only
Expand all | Collapse all

ogscript.asyncPost() and credentials

  • 1.  ogscript.asyncPost() and credentials

    Posted 07-13-2015 20:12
    Hi,

    If i sent a http command with credentials, i got a server HTTP response code: 401.

    For example if I sent something like "http://admin:admin@192.168.66.226/command = GetStatus", I get from Dashboard "Server returned HTTP response code: 401 for URL: http://admin:admin@192.168.6..."

    Is there a way to workaround this? we need to be able to set credentilals on the http command.

    Thanks

    Bruno


  • 2.  RE: ogscript.asyncPost() and credentials

    Posted 07-14-2015 13:34

    Unfortunately, there is currently no support for authenticated HTTP in DashBoard's asyncPost library.

    If the website uses simple authentication, I have an example that shows how one COULD fetch the content. It isn't as simple as asyncPost but you're welcome to take a look. Just save the code below to a new Custom Panel.

    `

    var encodedPassword = params.getValue('Username', 0);

    var path = params.getValue('Path', 0);

    var host = params.getValue('Host', 0);

    var port = params.getValue('Port', 0);
    var message = 'GET ' + path + ' HTTP/1.1rn' + //You will replace "/password-ok.php" with your desired path

    'Host: ' + host + 'rn' + //replace with your destination host/IP

    'Connection: closern' +

    'Authorization: Basic ' + encodedPassword + 'rn' + //this is a base64 encoding of 'test:test' [Username]:[password] you can do the encoding at https://www.base64encode.org/

    'rn?'; //HTTP headers end with a blank line. The question mark means that DashBoard will wait for a response

    function callback(success, sentData, resultString, exception)

    {

    ogscript.rename('content', resultString);

    }

    rosstalk.sendMessage(host, port, message, callback);

    `

    #DashBoard


  • 3.  RE: ogscript.asyncPost() and credentials

    Posted 07-14-2015 16:05
    Thanks James

    i have implemented this and it works very well.

    However, the result string that i get from the call back is always "HTTP/1.1 200 OK".

    What we really need is the content of the "Line-based text data".

    Do you know how we can get it?

    Thanks again for your help

    #DashBoard


  • 4.  RE: ogscript.asyncPost() and credentials

    Posted 07-14-2015 18:18

    That gets much more difficult...

    If I told you that it was almost so much trouble that it is hard to justify doing it, would you believe me?

    If not, here is the necessary code. It makes use of the listener tag to directly communicate back and forth with the server and read the response.

    If this gets completely mangled by the forum, contact me at jpeltzer@rossvideo.com and I'll send you the file.

    `

    if (event.isConnectEvent())

    {

    var host = params.getValue('Host', 0);

    var encodedPassword = params.getValue('Username', 0);

    var path = params.getValue('Path', 0);

    var message = 'GET ' + path + ' HTTP/1.1rn' + //You will replace "/password-ok.php" with your desired path

    'Host: ' + host + 'rn' + //replace with your destination host/IP

    'Connection: closern' +

    'Authorization: Basic ' + encodedPassword + 'rn' + //this is a base64 encoding of 'test:test' [Username]:[password] you can do the encoding at https://www.base64encode.org/

    'rn'; //HTTP headers end with a blank line.
    this.writeString(message, false);

    ogscript.rename('content', 'SENDING: ' + message);

    }

    else if (event.isMessageEvent())

    {

    try //No matter what happens, we want to close our connection so we put it in a try/catch block

    {

    var header = event.getBytesAsString(); //Get the first line of the header (contains response code

    var combinedHeader = header + 'n'; //Keep track of our combined header

    ogscript.rename('content', 'HEADER: ' + header); //DEBUG CODE

    if (header.indexOf('200') > 0) //If the response is OK

    {

    var messageParser = ogscript.createMessageParser(this.getInputStream()); //We are going to read directly from our connection

    var chunked = false; //HTTP supports chunked or content-length encoding

    var contentLength = 0; //We are going to read directly from our connection

    header = messageParser.readLine().trim(); //Read the next line in the header

    combinedHeader += header + 'n'; //Keeping track of the overall header for debugging

    while (header.length > 0) //The header ends with an empty line so we keep reading to build the header

    {

    if (header.indexOf('Content-Length') == 0) //If we get the Content-Length field, we know how many bytes to read

    {

    contentLength = parseInt(header.substr(('Content-Length').length + 1).trim());

    }

    else if (header.indexOf('Transfer-Encoding: chunked') == 0) //If we get the CHUNKED field, we know we need to read in chunks

    {

    chunked = true;

    }

    header = messageParser.readLine().trim(); //Get the next line of the header

    combinedHeader += header + 'n'; //Continue to build the header

    ogscript.rename('content', 'HEADER: ' + header);//DEBUG CODE

    }

    if (contentLength > 0) //If we have a specified content length, just read that string

    {

    var stringValue = messageParser.readString(contentLength);

    ogscript.rename('content', stringValue);

    }

    else if (chunked) //If we have a chunked encoding, we need to read each chunk

    {

    var stringValue = ''; //We're going to build our string

    while (true) //We're going to keep reading until we run out of chunks

    {

    var chunkLengthStr = messageParser.readLine().trim(); //Get the next chunk length line

    if (chunkLengthStr.length > 0)

    {

    var chunkLength = parseInt(chunkLengthStr, 16); //CHUNKS start with their length in hex

    if (chunkLength == 0) //The final chunk

    {

    break;

    }

    else

    {

    stringValue += messageParser.readString(chunkLength); //Read the actual chunk

    }

    messageParser.readLine(); //skip the CRLF at the end of the chunk

    }

    }

    ogscript.rename('content', stringValue); //We have a complete HTTP string! Yay!

    }

    else

    {

    ogscript.rename('content', combinedHeader); //Something unexpected happen, output the header so we can take a closer look

    }

    }

    }

    catch (e)

    {

    ogscript.rename('content', "EXCEPTION: " + e + ' ' + header);//DEBUG CODE

    }

    ogscript.getListenerById('http-sender').stop(); //We are done with the connection. Stop the listener.

    }

    var host = params.getValue('Host', 0);

    var port = params.getValue('Port', 0);
    ogscript.getListenerById('http-sender').connectTo(host, port);

    `

    #DashBoard


  • 5.  RE: ogscript.asyncPost() and credentials

    Posted 07-14-2015 18:19
    Note: You'll need DashBoard 7 to run the example as it makes use of the listener's `connectTo(host, port)` function which was added in that version.

    #DashBoard


  • 6.  RE: ogscript.asyncPost() and credentials

    Posted 07-14-2015 19:40
    You are the one!

    It's working well like this.

    Thank you so much.

    Bruno

    #DashBoard


  • 7.  RE: ogscript.asyncPost() and credentials

    Posted 07-17-2015 19:30
    Hi James,

    Is it possible to set a critical section in Dashboard to avoid 2 events be process at the same time?

    Thanks

    Bruno

    #DashBoard


  • 8.  RE: ogscript.asyncPost() and credentials

    Posted 07-17-2015 19:43
    There isn't really any way to synchronize things but you should be able to use some of the threading to your advantage.

    Can I get more info about the problem you're trying to solve?

    #DashBoard


  • 9.  RE: ogscript.asyncPost() and credentials

    Posted 07-20-2015 15:11
    Hi James,

    I'm sending http messages to our device.

    I have a timer that get the status on the device every 5 seconds.

    I also expose a start/stop button in the ui that send the http start and stop command.

    If the status message is sent at the time as the the start/stop command, the start/stop command will not reach the device. We have to click again on the button.

    I would like to implement a mechanism like mutex ior semaphore to prevent the device to get 2 commands at the same and block one one them until the previous one is completed.

    is it possible?

    Thanks

    Bruno

    #DashBoard


  • 10.  RE: ogscript.asyncPost() and credentials

    Posted 07-20-2015 18:08
    We don't have direct access to any of the advanced concurrency libraries inside of the DashBoard scripting layer so things will have to be a bit less elegant.

    One common approach would be to use ogscript.getObject([key]) and ogscript.putObject([key], [value]) to keep track of an array of messages to send. So, instead of calling "connectTo" directly, you would create a function called "sendMessage([path], [password])" that would look for a "messageQueue" and, if one is there, add this request to the end of the list. Otherwise, it creates a message queue and starts the listener.

    The listener itself would check at the bottom of reading the HTTP response and see if there is a next message to send.

    Again, we're getting into more complex code. One nice thing is that you'd have the ability to clear your queue when the user hits "stop".

    If you need more info than this to proceed, I can put together some kind of example to show you what I mean but won't be able to get to it until next week.

    #DashBoard


  • 11.  RE: ogscript.asyncPost() and credentials

    Posted 07-20-2015 18:39
    Thanks James,

    Yes, an example would speed up the implementation process.

    Thanks

    Bruno

    #DashBoard