Difference between revisions of "AJAX"

From MidasWiki
Jump to navigation Jump to search
Line 195: Line 195:
Warning: This is an example alarm
Warning: This is an example alarm
* BUG: return text is "\n" terminated, array created by javascript request.responseText.split('\n') will have an extra empty element at the end.

Revision as of 07:40, 22 August 2013


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.
            if (!equal_ustring(pitem, "format"))
            setparam(pitem, p);
  • BUG: confirm that values of "format0", etc are decoded correctly.


  • 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)) {
            rsprintf("Invalid password!");
    • ?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


  • 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


  • 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


  • JSONP: no
  • parameters:
    • ?odb=ODB path
  • returns:
   if (equal_ustring(getparam("cmd"), "jkey")) {
      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
  • example: TBW


  • JSONP: no
  • parameters:
    • ?n=number of messages to return
  • returns: output of midas.c::cm_msg_retrieve()
  • example:
$ curl "http://localhost:8080?cmd=jmsg&n=5"
Thu Aug 22 08:05:10 2013 [Logger,TALK] Warning: This is an example alarm
Thu Aug 22 08:06:13 2013 [mhttpd,TALK] Alarm: Run number became too large
Thu Aug 22 08:06:13 2013 [mhttpd,TALK] Warning: This is an example alarm
Thu Aug 22 08:07:15 2013 [fejrpc,TALK] Alarm: Run number became too large
Thu Aug 22 08:07:15 2013 [fejrpc,TALK] Warning: This is an example alarm
  • BUG: last message is not "\n" terminated (violation of the HTTP protocol?)


  • JSONP: no
  • parameters:
    • &msg=message text
  • returns: string "Message successfully created\n"
  • example:
$ curl "http://localhost:8080?cmd=jgenmsg&msg=test+message"
Message successfully created
  • BUG: "Message successfully created\n" is returned even if the "msg" parameter is missing and regardless of cm_msg() error return code.


  • JSONP: no
  • parameters: no
  • returns:
    • text from alarm.c::al_get_alarms()
    • is "\n" separated list of alarms
    • alarm format is "class: text", where "class" is the alarm class, "text" is the alarm message from ODB /Alarms/alarms/xxx/
  • example:
$ curl http://localhost:8080?cmd=jalm
Alarm: Run number became too large
Warning: This is an example alarm
  • BUG: return text is "\n" terminated, array created by javascript request.responseText.split('\n') will have an extra empty element at the end.