Difference between revisions of "AJAX"

From MidasWiki
Jump to navigation Jump to search
Line 333: Line 333:
 
** &encoding=<code>encoding</code> only JSON is implemented
 
** &encoding=<code>encoding</code> only JSON is implemented
 
** &callback=<code>string</code> name of the JSONP callback function
 
** &callback=<code>string</code> name of the JSONP callback function
** &odb=<code>ODB path</code> single jcreate function
+
** &odb=<code>ODB path</code> single jrename function
** &odb0=<code>ODB path</code> multiple jcreate function
+
** &odb0=<code>ODB path</code> multiple jrename function
** &odb<code>N</code>=<code>ODB path</code> ODB paths for multiple copy
+
** &odb<code>N</code>=<code>ODB path</code> ODB paths for multiple rename
** &type=<code>TID_type</code> single jcreate: integer "type" argument to db_create_key()
+
** &name=<code>string</code> single rename: new key "name" argument to db_rename_key()
** &type<code>N</code>=<code>TID_type</code> same for multiple jcreate
+
** &name<code>N</code>=<code>string</code> same for multiple rename
  
* single jcreate returns:
+
* single jrename returns:
** integer status value from db_create_key()
+
** integer status value from db_rename_key()
  
* multiple jcreate returns:
+
* multiple jrename returns:
 
** data encoded as JSON, i.e.: [ 1, 312, 1 ]
 
** data encoded as JSON, i.e.: [ 1, 312, 1 ]
  
 
* examples:
 
* examples:
  
** single jcreate:
+
** single jrename:
 
<pre>
 
<pre>
$ curl "http://localhost:8080?cmd=jcreate&odb=/test/foo&type=7"
+
$ curl "http://localhost:8080?cmd=jrename&odb=/test/foo&name=foofoo"
 
1
 
1
 
</pre>
 
</pre>
  
** multiple jcreate with JSON-P encoding:
+
** multiple jrename:
(note how /nonexistant is created with an invalid "type")
 
 
<pre>
 
<pre>
$ curl "http://localhost:8080?cmd=jcreate&odb0=/test/foo&type0=7&odb1=/nonexistant&type1=00&odb2=/test/bar&type2=12&encoding=json&callback=aaa"
+
$ curl "http://localhost:8080?cmd=jrename&odb0=/test/foo&name0=foofoo&odb1=/test/bar&name1=barbar"
aaa([ 1, 309, 1 ]);
+
[ 1, 1 ]
 
</pre>
 
</pre>
  

Revision as of 08:04, 4 September 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.

mhttpd AJAX functions are invoked using the "cmd" parameter with value equal to the name of the AJAX request, for example, to call "jmsg", do:

curl "http://localhost:8080?cmd=jmsg&n=5"

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 same as "encoding" but subject to an mhttpd URI percent-encoding bug (it is not URI-decoded)
    • &encoding=encoding 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 "encoding" (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:
    • encoding="odb", separator is string "$#----#$\n", non existant keys return <DB_NO_KEY>"
    • encoding="xml", outer XML element is <jcopy>, each ODB path is wrapped in a element, non existant keys are encoded as <DB_NO_KEY/>
    • encoding="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 "encoding" (and "format") parameter:
    • encoding="odb" - returns data encoded by db_copy()
    • encoding="xml" - returns data encoded by db_copy_xml()
    • encoding="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.
  • examples:
    • single jcopy in ODB format:
$ curl "http://localhost:8080?cmd=jcopy&odb=/runinfo&encoding=odb"
[.]
State = INT : 1
Online Mode = INT : 1
Run number = INT : 0
Transition in progress = INT : 0
Start abort = INT : 0
Requested transition = INT : 0
Start time = STRING : [32] Tue Sep 09 15:04:42 1997
Start time binary = DWORD : 0
Stop time = STRING : [32] Tue Sep 09 15:04:42 1997
Stop time binary = DWORD : 0
    • single jcopy in XML format:
$ curl "http://localhost:8080?cmd=jcopy&odb=/runinfo&encoding=xml"
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- created by MXML on Thu Aug 22 09:53:02 2013 -->
<odb root="/Runinfo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://midas.psi.ch/odb.xsd">
  <key name="State" type="INT">1</key>
  <key name="Online Mode" type="INT">1</key>
  <key name="Run number" type="INT">0</key>
  <key name="Transition in progress" type="INT">0</key>
  <key name="Start abort" type="INT">0</key>
  <key name="Requested transition" type="INT">0</key>
  <key name="Start time" type="STRING" size="32">Tue Sep 09 15:04:42 1997</key>
  <key name="Start time binary" type="DWORD">0</key>
  <key name="Stop time" type="STRING" size="32">Tue Sep 09 15:04:42 1997</key>
  <key name="Stop time binary" type="DWORD">0</key>
</odb>
    • single jcopy on JSON format:
$ curl "http://localhost:8080?cmd=jcopy&odb=/runinfo&encoding=json"
{
  "State/key" : { "type" : 7, "last_written" : 1377182821 },
  "State" : 1,
  "Online Mode/key" : { "type" : 7, "last_written" : 1377182821 },
  "Online Mode" : 1,
  "Run number/key" : { "type" : 7, "last_written" : 1377182821 },
  "Run number" : 0,
  "Transition in progress/key" : { "type" : 7, "last_written" : 1377182821 },
  "Transition in progress" : 0,
  "Start abort/key" : { "type" : 7, "last_written" : 1377182821 },
  "Start abort" : 0,
  "Requested transition/key" : { "type" : 7, "last_written" : 1377182821 },
  "Requested transition" : 0,
  "Start time/key" : { "type" : 12, "item_size" : 32, "last_written" : 1377182821 },
  "Start time" : "Tue Sep 09 15:04:42 1997",
  "Start time binary/key" : { "type" : 6, "last_written" : 1377182821 },
  "Start time binary" : "0x00000000",
  "Stop time/key" : { "type" : 12, "item_size" : 32, "last_written" : 1377182821 },
  "Stop time" : "Tue Sep 09 15:04:42 1997",
  "Stop time binary/key" : { "type" : 6, "last_written" : 1377182821 },
  "Stop time binary" : "0x00000000"
}
    • multiple jcopy in JSON format:
$ curl "http://localhost:8080?cmd=jcopy&odb0=/experiment/name&odb1=/runinfo&encoding=json"
[
{
"Name/key" : { "type" : 12, "item_size" : 32, "last_written" : 1377190203 },
"Name" : "javascript1"
}
,
{
  "State/key" : { "type" : 7, "last_written" : 1377182821 },
  "State" : 1,
  "Online Mode/key" : { "type" : 7, "last_written" : 1377182821 },
  "Online Mode" : 1,
  "Run number/key" : { "type" : 7, "last_written" : 1377182821 },
  "Run number" : 0,
  "Transition in progress/key" : { "type" : 7, "last_written" : 1377182821 },
  "Transition in progress" : 0,
  "Start abort/key" : { "type" : 7, "last_written" : 1377182821 },
  "Start abort" : 0,
  "Requested transition/key" : { "type" : 7, "last_written" : 1377182821 },
  "Requested transition" : 0,
  "Start time/key" : { "type" : 12, "item_size" : 32, "last_written" : 1377182821 },
  "Start time" : "Tue Sep 09 15:04:42 1997",
  "Start time binary/key" : { "type" : 6, "last_written" : 1377182821 },
  "Start time binary" : "0x00000000",
  "Stop time/key" : { "type" : 12, "item_size" : 32, "last_written" : 1377182821 },
  "Stop time" : "Tue Sep 09 15:04:42 1997",
  "Stop time binary/key" : { "type" : 6, "last_written" : 1377182821 },
  "Stop time binary" : "0x00000000"
}
]
    • multiple jcopy in condensed JSON encoding with JSONP wrapper:
$ curl "http://localhost:8080?cmd=jcopy&odb0=/experiment/name&odb1=/runinfo&encoding=json-p-nokeys&callback=mycallback"
mycallback([
{
"Name/last_written" : 1377190203,
"Name" : "javascript1"
}
,
{
  "State/last_written" : 1377182821,
  "State" : 1,
  "Online Mode/last_written" : 1377182821,
  "Online Mode" : 1,
  "Run number/last_written" : 1377182821,
  "Run number" : 0,
  "Transition in progress/last_written" : 1377182821,
  "Transition in progress" : 0,
  "Start abort/last_written" : 1377182821,
  "Start abort" : 0,
  "Requested transition/last_written" : 1377182821,
  "Requested transition" : 0,
  "Start time/last_written" : 1377182821,
  "Start time" : "Tue Sep 09 15:04:42 1997",
  "Start time binary/last_written" : 1377182821,
  "Start time binary" : "0x00000000",
  "Stop time/last_written" : 1377182821,
  "Stop time" : "Tue Sep 09 15:04:42 1997",
  "Stop time binary/last_written" : 1377182821,
  "Stop time binary" : "0x00000000"
}
]
);

jkey

  • JSONP: no
  • parameters:
    • &odb=ODB path
  • returns:
    • "<DB_NO_KEY>" if key does not exist
    • MIDAS "KEY" structure (see midas.h)
         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);
  • example:
$ curl "http://localhost:8080?cmd=jkey&odb=/runinfo/run+number"
Run number
TID_INT
1
4
1377182821
  • BUG: last entry is not "\n" terminated
  • BUG: there is no check for db_get_key() error return

jcreate

  • git branch: feature/ajax
  • JSONP: yes
  • parameters:
    • &encoding=encoding only JSON is implemented
    • &callback=string name of the JSONP callback function
    • &odb=ODB path single jcreate function
    • &odb0=ODB path multiple jcreate function
    • &odbN=ODB path ODB paths for multiple copy
    • &type=TID_type single jcreate: integer "type" argument to db_create_key()
    • &typeN=TID_type same for multiple jcreate
  • single jcreate returns:
    • integer status value from db_create_key()
  • multiple jcreate returns:
    • data encoded as JSON, i.e.: [ 1, 312, 1 ]
  • examples:
    • single jcreate:
$ curl "http://localhost:8080?cmd=jcreate&odb=/test/foo&type=7"
1
    • multiple jcreate with JSON-P encoding:

(note how /nonexistant is created with an invalid "type")

$ curl "http://localhost:8080?cmd=jcreate&odb0=/test/foo&type0=7&odb1=/nonexistant&type1=00&odb2=/test/bar&type2=12&encoding=json&callback=aaa"
aaa([ 1, 309, 1 ]);

jrename

  • git branch: feature/ajax
  • JSONP: yes
  • parameters:
    • &encoding=encoding only JSON is implemented
    • &callback=string name of the JSONP callback function
    • &odb=ODB path single jrename function
    • &odb0=ODB path multiple jrename function
    • &odbN=ODB path ODB paths for multiple rename
    • &name=string single rename: new key "name" argument to db_rename_key()
    • &nameN=string same for multiple rename
  • single jrename returns:
    • integer status value from db_rename_key()
  • multiple jrename returns:
    • data encoded as JSON, i.e.: [ 1, 312, 1 ]
  • examples:
    • single jrename:
$ curl "http://localhost:8080?cmd=jrename&odb=/test/foo&name=foofoo"
1
    • multiple jrename:
$ curl "http://localhost:8080?cmd=jrename&odb0=/test/foo&name0=foofoo&odb1=/test/bar&name1=barbar"
[ 1, 1 ]

jdelete

  • git branch: feature/ajax
  • JSONP: yes
  • parameters:
    • &encoding=encoding only JSON is implemented
    • &callback=string name of the JSONP callback function
    • &odb=ODB path single jdelete function
    • &odb0=ODB path multiple jdelete function
    • &odbN=ODB path ODB paths for multiple jdelete
  • single jdelete returns:
    • integer status value from db_delete_key()
  • multiple jdelete returns:
    • data encoded as JSON, i.e.: [ 1, 312, 1 ]
  • examples:
    • single jdelete:
$ curl "http://localhost:8080?cmd=jdelete&odb=/test/foo"
1
    • multiple jdelete with JSON-P encoding:
$ curl "http://localhost:8080?cmd=jdelete&odb0=/test/foo&odb1=/nonexistant&odb2=/test/bar&encoding=json&callback=aaa"
aaa([ 1, 312, 1 ]);

jmsg

  • 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?)

jgenmsg

  • 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.

jalm

  • 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.