Difference between revisions of "AJAX"

From MidasWiki
Jump to navigation Jump to search
Line 113: Line 113:
 
** format="odb", separator is string "$#----#$\n", non existant keys return <DB_NO_KEY>"
 
** format="odb", separator is string "$#----#$\n", non existant keys return <DB_NO_KEY>"
 
** format="xml", outer XML element is <jcopy>, each ODB path is wrapped in a <data> element, non existant keys are encoded as <DB_NO_KEY/>
 
** format="xml", outer XML element is <jcopy>, each ODB path is wrapped in a <data> element, non existant keys are encoded as <DB_NO_KEY/>
** format="json", data is encoded as a JSON array - "[ path0, path1, ... ]", non existant keys are encoded as ""{ \"/error\" : %d }\n" with the error return value of db_find_key().
+
** format="json", data is encoded as a JSON array - "[ path0, path1, ... ]", non existant keys are encoded as "{ "/error" : %d }" with the error return value of db_find_key().
  
 
* jcopy "format" parameter:
 
* jcopy "format" parameter:

Revision as of 10:43, 21 August 2013

About

This page will document the MIDAS AJAX functions accessible through special mhttpd URLs.

These AJAX functions are typically accessed from browser javascript using the MIDAS mhttpd.js library.

As originally written, code snippets are taken from mhttpd.cxx git commit db4602dc2ed63674413f92cc92f49f183bd14225, 21 Aug 2013.

URL percent-encoding

All parameter names and parameter values (?name=value) are passed through urlDecode.

Encoding used by urlDecode:

  • %HH - decoded as char value 0xHH (hex encoding)
  • "+" - decoded as " " ("plus" decoded as "space")
  • other chars decoded as themselves

This is consistent with URL encoding rules specified by http://tools.ietf.org/html/rfc3986

On the client side (mhttpd.js), parameter values should be encoded using the JavaScript encodeURIComponent() function.

  • BUG: value of parameter "format" is not passed through urlDecode (git commit db4602dc2ed63674413f92cc92f49f183bd14225, 21 Aug 2013), this is consistent with mhttpd.js which does not urlEncode the values. Values of "format0", etc used by ODBMGet() are decoded as expected.
            urlDecode(pitem);
            if (!equal_ustring(pitem, "format"))
               urlDecode(p);
            setparam(pitem, p);
  • BUG: confirm that values of "format0", etc are decoded correctly.

jset

  • JSONP: no
  • parameters:
    • ?pnam=xxx - cookie_cpwd to match value of ODB /Custon/Pwd/xxx. How is this supposed to work??? How to use it???
      if (*getparam("pnam")) {
         sprintf(ppath, "/Custom/Pwd/%s", getparam("pnam"));
         str[0] = 0;
         db_get_value(hDB, 0, ppath, str, &size, TID_STRING, TRUE);
         if (!equal_ustring(cookie_cpwd, str)) {
            show_text_header();
            rsprintf("Invalid password!");
            return;
         }
      }
    • ?odb=ODB path
    • ?value=data
    • ?type=integer - if ODB path does not exist, it is created, using "type" as the MIDAS TID_xxx data type
    • ?len=integer - when ODB path is created, if "len" is not zero, it is made an array of "len" size, with all values set to "value". Except if "type" is TID_STRING, there is no array created, but "len" is used as the ODB string length.
  • ODB path encoding for arrays:
    • /path/array[i] - write array element [i]
    • /path/array - write array element [0]
    • /path/array[*] - write as many array elements as there are comma-separated entries in "value". There is no check against existing array length - db_set_data_index() will grow the array as neeed.
  • example: TBW

jget

  • JSONP: no
  • parameters:
    • ?odb=ODB path
    • ?format=sprintf format
    • ?odb0=ODB path - activates the multiple jget function
    • ?odbN=ODB path
    • ?formatN=sprintf format
    • ?name=integer - non-zero value enables output of key names
  • returns:
    • single jget returns the text from output_key()
    • multiple jget returns the values of all given ODB paths (?odb0=path?format0=format?odb1=path?format1=format?...) separated by the string "$#----#$\n".
    • <DB_NO_KEY> for non-existant ODB paths (db_find_key() != DB_SUCCESS)
    • <DB_OUT_OF_RANGE> for out of bounds array elements
  • ODB array path encoding:
    • /path/array[i] -> index=i
    • /path/array[*] -> index=-1
    • /path/array -> index=0
    • then it is passed to output_key()
  • encoding by mhttpd.cxx output_key():
    • TID_KEY: all subkeys are enumerated and passed to output_key() with "index" set to "-1". Output for all subkeys is concatenated without any separators.
    • arrays or simple variables (index=-1): "v0\nv1\n...", if "name" is "1", then: "key[0]: v0\nkey[1]: v1\n..."
    • array elements or simple variables (index!=-1): "value\n", if "name" is "1", then: "key[i]: valuei\n"
  • BUG: output_key() has no check against negative array indices - passed directly to db_sprintf()/db_sprintff() which also has no array bounds check
  • BUG: output_key() has no check for db_get_data() success or failure, no handling of DB_TRUNCATED.
  • BUG: some versions of mhttpd do not percent-encode the value of the "format" parameter (see section about URL encoding). Values of "format0", etc are decoded as expected.
  • BUG: output_key() ignores it's "format" argument except when encoding arrays. Instead, for encoding of simple variables the value of URL "format" parameter is used unconditionally. This means that the ODBMGet() format[] arguments are only applied to arrays, simple variables use the default format.
  • example: TBW

jcopy

  • JSONP: yes
  • parameters:
    • ?format=format see below
    • ?callback=string name of the JSONP callback function
    • ?odb=ODB path single jcopy function
    • ?odb0=ODB path multiple jcopy function
    • ?odbN=ODB path ODB paths for multiple copy
  • single jcopy returns:
    • data encoded according to "format" (see below)
    • <DB_NO_KEY> is ODB path does not exist (db_find_key() != DB_SUCCESS)
  • multiple jcopy returns:
    • each ODB path is encoded same for single jcopy, with following separators:
    • format="odb", separator is string "$#----#$\n", non existant keys return <DB_NO_KEY>"
    • format="xml", outer XML element is <jcopy>, each ODB path is wrapped in a element, non existant keys are encoded as <DB_NO_KEY/>
    • format="json", data is encoded as a JSON array - "[ path0, path1, ... ]", non existant keys are encoded as "{ "/error" : %d }" with the error return value of db_find_key().
  • jcopy "format" parameter:
    • format="odb" - returns data encoded by db_copy()
    • format="xml" - returns data encoded by db_copy_xml()
    • format="json" - returns data encoded by db_copy_json(), with these options:
      • "json-p" - data is wrapped by "callback(data);\n"
      • "json-nofollowlinks" - ODB links are encoded as links not as values pointed to by links
      • "json-nokeys" - only values are encoded, ODB keys are omitted, except for "last_written"
      • "json-nolastwritten" - value of ODB key "last_written" is omitted, too.


  • example: TBW

jkey

  • JSONP: no
  • parameters:
    • ?odb=ODB path
  • returns:
   if (equal_ustring(getparam("cmd"), "jkey")) {
      show_text_header();
      if (isparam("odb") && db_find_key(hDB, 0, getparam("odb"), &hkey) == DB_SUCCESS) {
         db_get_key(hDB, hkey, &key);
         rsprintf("%s\n", key.name);
         rsprintf("TID_%s\n", rpc_tid_name(key.type));
         rsprintf("%d\n", key.num_values);
         rsprintf("%d\n", key.item_size);
         rsprintf("%d", key.last_written);
      } else
         rsputs("<DB_NO_KEY>");
      return;
   }
  • example: TBW

jmsg

  • JSONP: no
  • parameters:
    • ?n=number of messages to return
  • returns: output of cm_msg_retrieve()
   if (equal_ustring(getparam("cmd"), "jmsg")) {
      i = 1;
      if (*getparam("n"))
         i = atoi(getparam("n"));
      
      show_text_header();
      cm_msg_retrieve(i, str, sizeof(str));
      rsputs(str);
      return;
   }
  • example: TBW

jgenmsg

  • JSONP: no
  • parameters:
    • ?msg=message text
  • returns: ???
   if (equal_ustring(getparam("cmd"), "jalm")) {
      show_text_header();
      al_get_alarms(str, sizeof(str));
      rsputs(str);
      return;
   }
  • example: TBW

jalm

  • JSONP: no
  • parameters: no
  • returns: text from al_get_alarms()
   if (equal_ustring(getparam("cmd"), "jalm")) {
      show_text_header();
      al_get_alarms(str, sizeof(str));
      rsputs(str);
      return;
   }
  • example: TBW