XML-RPC

XML-RPC(中文版)
发布:2009-11-05 15:41 | 作者:esailor | 来源:本站 | 查看:144次 | 字号: 小 中 大

XML-RPC规范(中文版)

本规范说明的xml-rpc协议实现userland frontier 5.1。

关于非技术性说明,请访问xml-rpc for newbies。

文件提供了实现xml-rpc所需要的所有信息。

前言

xml-rpc是一种基于internet的远程函数调用协议。

xml-rpc消息都是http-post请求。请求的主要部分的xml。服务器端执行后的返回结果同样也是xml格式。

函数调用的参数能是scalars, numbers, strings, dates等等;也能是混合型的记录和结构体。

request请求样式

下面是个xml-rpc请求的例子:

post /rpc2 http/1.0

user-agent: frontier/5.1.2 (winnt)

host: betty.userland.com

content-type: text/xml

content-length: 181

<?xml version="1.0"?>

<methodcall>

<methodname>examples.getstatename</methodname>

<params>

<param>

<value><i4>41</i4></value>

</param>

</params>

</methodcall>

关于请求头

第一行的uri格式不是特定的。能为空,如果服务器只处理xml-rpc请求甚至能只是简单的一个斜线。可是,如果服务器除了xml-rpc外还提供其他的http请求,uri能帮助我们把请求指向特定的xml-rpc服务。

user-agent和host项是必须的。

content-type的值必须是text/xml.

content-length必须指定,而且必须是正确的值。

有效的格式

xml-rpc具有和xml相同的有效格式,并且是个<methodcall>结构。

<methodcall>必须包含一个值为字符型的<methodname>子元素,用来表明被调用的方法。这个字符必须符合以下规定:大小写字母、数字0-9、下划线、点、冒号和斜线。至于怎么解释这个字符串将有服务器端来决定。

例如,methodname能是个包含执行request请求的文件的名字,能是数据表中列的名字,还能是表示目录和文件结构的路径。

如果远程调用接受参数,<methodcall>就必须包含<params>子元素。<params>能包含任意个<param>元素,每个<param>包含一个<value>子元素。

scalar <value>s <value>

<value>值被嵌入类型标签中,支持的类型如下表:

tag type example

<i4> or <int> four-byte signed integer -12

<boolean> 0 (false) or 1 (true) 1

<string> string hello world

<double> double-precision signed floating point number -12.214

<datetime.iso8601> date/time 19980717t14:08:55

<base64> base64-encoded binary ew91ignhbid0ihjlywqgdghpcye=

如果没有指定类型,默认为字符串。

<struct>s

参数值能是<struct>。

每个<struct>包含若干<member>,每个<member>包含一个<name>和一个<value>.

如果所示为包含两个值的<struct>

<struct>

<member>

<name>lowerbound</name>

<value><i4>18</i4></value>

</member>

<member>

<name>upperbound</name>

<value><i4>139</i4></value>

</member>

</struct>

<struct>是能递归使用的,所有<value>都里还能<struct>或其他所有类型,包括后面将要说明的<array><array>s

值能个<array>

一个<array>简单的有一个<data>元素。<data>能是所有合法类型。

下面是个有4个值的array:

<array>

<data>

<value><i4>12</i4></value>

<value><string>egypt</string></value>

<value><boolean>0</boolean></value>

<value><i4>-31</i4></value>

</data>

</array>

<array> elements do not have names.

<array> 元素没有名字。

你能混合使用上面列出的几种类型。

<arrays>能递归使用,其值能是<array>或其他类型,包括上面说明的<strut>。

response应答样式

下面是个 xml-rpc请求:

http/1.1 200 ok

connection: close

content-length: 158

content-type: text/xml

date: fri, 17 jul 1998 19:55:08 gmt

server: userland frontier/5.1.2-winnt

<?xml version="1.0"?>

<methodresponse>

<params>

<param>

<value><string>south dakota</string></value>

</param>

</params>

</methodresponse>

respnse应答格式

除非底层操作出现错,否则总是返回200 ok.

content-type是text/xml。必须设置content-length,并且必须是正确的值。

应到内容是个简单的xml,可是是<methodresponse>包含一个<params>,<params>包含一个<param>,<param>包含一个<value><methodresponse>可能含有一个<fault>标签。<fault>的值为<struct>类型,<struct>有两个元素,值为<int><faultcode>和值为<string><faultstring><methodresponse>不能既有<fault>又有<params>。

fault example

http/1.1 200 ok

connection: close

content-length: 426

content-type: text/xml

date: fri, 17 jul 1998 19:55:02 gmt

server: userland frontier/5.1.2-winnt

<?xml version="1.0"?>

<methodresponse>

<fault>

<value>

<struct>

<member>

<name>faultcode</name>

<value><int>4</int></value>

</member>

<member>

<name>faultstring</name>

<value><string>too many parameters.</string></value>

</member>

</struct>

</value>

</fault>

</methodresponse>

strategies/goals

firewalls. the goal of this protocol is to lay a compatible foundation across different environments, no new power is provided beyond the capabilities of the cgi interface. firewall software can watch for posts whose content-type is text/xml.

discoverability. we wanted a clean, extensible format thats very simple. it should be possible for an html coder to be able to look at a file containing an xml-rpc procedure call, understand what its doing, and be able to modify it and have it work on the first or second try.

easy to implement. we also wanted it to be an easy to implement protocol that could quickly be adapted to run in other environments or on other operating systems.

updated 1/21/99 dw

the following questions came up on the userland discussion group as xml-rpc was being implemented in python.

the response format section says "the body of the response is a single xml structure, a <methodresponse>, which can contain a single <params>..." this is confusing. can we leave out the <params>?

no you cannot leave it out if the procedure executed successfully. there are only two options, either a response contains a <params> structure or it contains a <fault> structure. thats why we used the word "can" in that sentence.

is "boolean" a distinct data type, or can boolean values be interchanged with integers (e.g. zero=false, non-zero=true)?

yes, boolean is a distinct data type. some languages/environments allow for an easy coercion from zero to false and one to true, but if you mean true, send a boolean type with the value true, so your intent cant possibly be misunderstood.

what is the legal syntax (and range) for integers? how to deal with leading zeros? is a leading plus sign allowed? how to deal with whitespace?

an integer is a 32-bit signed number. you can include a plus or minus at the beginning of a string of numeric characters. leading zeros are collapsed. whitespace is not permitted. just numeric characters preceeded by a plus or minus.

what is the legal syntax (and range) for floating point values (doubles)? how is the exponent represented? how to deal with whitespace? can infinity and "not a number" be represented?

there is no representation for infinity or negative infinity or "not a number". at this time, only decimal point notation is allowed, a plus or a minus, followed by any number of numeric characters, followed by a period and any number of numeric characters. whitespace is not allowed. the range of allowable values is implementation-dependent, is not specified.

what characters are allowed in strings? non-printable characters? null characters? can a "string" be used to hold an arbitrary chunk of binary data?

any characters are allowed in a string except < and &, which are encoded as &lt; and &amp;. a string can be used to encode binary data.

does the "struct" element keep the order of keys. or in other words, is the struct "foo=1, bar=2" equivalent to "bar=2, foo=1" or not?

the struct element does not preserve the order of the keys. the two structs are equivalent.

can the <fault> struct contain other members than <faultcode> and <faultstring>? is there a global list of faultcodes? (so they can be mapped to distinct exceptions for languages like python and java)?

a <fault> struct may not contain members other than those specified. this is true for all other structures. we believe the specification is flexible enough so that all reasonable data-transfer needs can be accomodated within the specified structures. if you believe strongly that this is not true, please post a message on the discussion group.

there is no global list of fault codes. it is up to the server implementer, or higher-level standards to specify fault codes.

what timezone should be assumed for the datetime.iso8601 type? utc? localtime?

dont assume a timezone. it should be specified by the server in its documentation what assumptions it makes about timezones.

additions

<base64> type. 1/21/99 dw.

updated 6/30/03 dw

removed "ascii" from definition of string.

changed copyright dates, below, to 1999-2003 from 1998-99.

copyright and disclaimer

? copyright 1998-2003 userland software. all rights reserved.

this document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and these paragraphs are included on all such copies and derivative works.

this document may not be modified in any way, such as by removing the copyright notice or references to userland or other organizations. further, while these copyright restrictions apply to the written xml-rpc specification, no claim of ownership is made by userland to the protocol it describes. any party may, for commercial or non-commercial purposes, implement this protocol without royalty or license fee to userland. the limited permissions granted herein are perpetual and will not be revoked by userland or its successors or assigns.

this document and the information contained herein is provided on an "as is" basis and userland disclaims all warranties, express or implied, including but not limited to any warranty that the use of the information herein will not infringe any rights or any implied warranties of merchantability or fitness for a particular purpose. 
posted @ 2012-08-22 10:09  Anjey  阅读(274)  评论(0编辑  收藏  举报