| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2005, 2006 Daniel Ehrenberg | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-08-29 05:33:05 -04:00
										 |  |  | USING: accessors kernel xml arrays math generic http.client | 
					
						
							|  |  |  | combinators hashtables namespaces io base64 sequences strings | 
					
						
							|  |  |  | calendar xml.data xml.writer xml.utilities assocs math.parser | 
					
						
							|  |  |  | debugger calendar.format math.order ;
 | 
					
						
							| 
									
										
										
										
											2008-04-26 12:03:41 -04:00
										 |  |  | IN: xml-rpc | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! * Sending RPC requests | 
					
						
							|  |  |  | ! TODO: time | 
					
						
							|  |  |  | ! The word for what this does is "serialization"! Wow! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: item>xml ( object -- xml )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: integer item>xml | 
					
						
							|  |  |  |     dup 31 2^ neg 31 2^ 1 - between? | 
					
						
							|  |  |  |     [ "Integers must fit in 32 bits" throw ] unless
 | 
					
						
							|  |  |  |     number>string "i4" build-tag ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-29 05:33:05 -04:00
										 |  |  | UNION: boolean t POSTPONE: f ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: boolean item>xml | 
					
						
							|  |  |  |     "1" "0" ? "boolean" build-tag ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: float item>xml | 
					
						
							|  |  |  |     number>string "double" build-tag ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: string item>xml ! This should change < and & | 
					
						
							|  |  |  |     "string" build-tag ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : struct-member ( name value -- tag )
 | 
					
						
							|  |  |  |     swap dup string?
 | 
					
						
							|  |  |  |     [ "Struct member name must be string" throw ] unless
 | 
					
						
							|  |  |  |     "name" build-tag swap
 | 
					
						
							|  |  |  |     item>xml "value" build-tag | 
					
						
							|  |  |  |     2array "member" build-tag* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: hashtable item>xml | 
					
						
							| 
									
										
										
										
											2007-10-16 04:15:16 -04:00
										 |  |  |     [ struct-member ] { } assoc>map
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     "struct" build-tag* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: array item>xml | 
					
						
							|  |  |  |     [ item>xml "value" build-tag ] map
 | 
					
						
							|  |  |  |     "data" build-tag* "array" build-tag ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: base64 string ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <base64> base64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: base64 item>xml | 
					
						
							| 
									
										
										
										
											2008-09-02 13:15:08 -04:00
										 |  |  |     string>> >base64 "base64" build-tag ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : params ( seq -- xml )
 | 
					
						
							|  |  |  |     [ item>xml "value" build-tag "param" build-tag ] map
 | 
					
						
							|  |  |  |     "params" build-tag* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : method-call ( name seq -- xml )
 | 
					
						
							|  |  |  |     params >r "methodName" build-tag r> | 
					
						
							|  |  |  |     2array "methodCall" build-tag* build-xml ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : return-params ( seq -- xml )
 | 
					
						
							|  |  |  |     params "methodResponse" build-tag build-xml ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : return-fault ( fault-code fault-string -- xml )
 | 
					
						
							|  |  |  |     [ "faultString" set "faultCode" set ] H{ } make-assoc item>xml | 
					
						
							|  |  |  |     "value" build-tag "fault" build-tag "methodResponse" build-tag | 
					
						
							|  |  |  |     build-xml ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: rpc-method name params ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <rpc-method> rpc-method | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: rpc-response params ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <rpc-response> rpc-response | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: rpc-fault code string ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <rpc-fault> rpc-fault | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: send-rpc ( rpc -- xml )
 | 
					
						
							|  |  |  | M: rpc-method send-rpc | 
					
						
							| 
									
										
										
										
											2008-09-02 13:36:15 -04:00
										 |  |  |     [ name>> ] [ params>> ] bi method-call ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | M: rpc-response send-rpc | 
					
						
							| 
									
										
										
										
											2008-09-02 13:36:15 -04:00
										 |  |  |     params>> return-params ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | M: rpc-fault send-rpc | 
					
						
							| 
									
										
										
										
											2008-09-02 13:36:15 -04:00
										 |  |  |     [ code>> ] [ string>> ] bi return-fault ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! * Recieving RPC requests | 
					
						
							|  |  |  | ! this needs to have much better error checking | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: server-error tag message ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : server-error ( tag message -- * )
 | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:27 -04:00
										 |  |  |     \ server-error boa throw ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: server-error error. | 
					
						
							|  |  |  |     "Error in XML supplied to server" print
 | 
					
						
							| 
									
										
										
										
											2008-09-02 13:36:15 -04:00
										 |  |  |     "Description: " write dup message>> print
 | 
					
						
							|  |  |  |     "Tag: " write tag>> xml>string print ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PROCESS: xml>item ( tag -- object )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TAG: string xml>item | 
					
						
							|  |  |  |     children>string ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TAG: i4/int/double xml>item | 
					
						
							|  |  |  |     children>string string>number ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TAG: boolean xml>item | 
					
						
							|  |  |  |     dup children>string { | 
					
						
							|  |  |  |         { [ dup "1" = ] [ 2drop t ] } | 
					
						
							|  |  |  |         { [ "0" = ] [ drop f ] } | 
					
						
							| 
									
										
										
										
											2008-04-11 13:57:43 -04:00
										 |  |  |         [ "Bad boolean" server-error ] | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : unstruct-member ( tag -- )
 | 
					
						
							|  |  |  |     children-tags first2
 | 
					
						
							|  |  |  |     first-child-tag xml>item | 
					
						
							|  |  |  |     >r children>string r> swap set ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TAG: struct xml>item | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         children-tags [ unstruct-member ] each
 | 
					
						
							|  |  |  |     ] H{ } make-assoc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TAG: base64 xml>item | 
					
						
							|  |  |  |     children>string base64> <base64> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TAG: array xml>item | 
					
						
							|  |  |  |     first-child-tag children-tags | 
					
						
							|  |  |  |     [ first-child-tag xml>item ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : params>array ( tag -- array )
 | 
					
						
							|  |  |  |     children-tags | 
					
						
							|  |  |  |     [ first-child-tag first-child-tag xml>item ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : parse-rpc-response ( xml -- array )
 | 
					
						
							|  |  |  |     first-child-tag params>array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : parse-method ( xml -- string array )
 | 
					
						
							| 
									
										
										
										
											2008-09-02 13:36:15 -04:00
										 |  |  |     children-tags first2
 | 
					
						
							|  |  |  |     [ children>string ] [ params>array ] bi* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : parse-fault ( xml -- fault-code fault-string )
 | 
					
						
							|  |  |  |     first-child-tag first-child-tag first-child-tag | 
					
						
							|  |  |  |     xml>item [ "faultCode" get "faultString" get ] bind ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : receive-rpc ( xml -- rpc )
 | 
					
						
							| 
									
										
										
										
											2008-08-29 05:33:05 -04:00
										 |  |  |     dup main>> dup "methodCall" =
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     [ drop parse-method <rpc-method> ] [ | 
					
						
							|  |  |  |         "methodResponse" = [ | 
					
						
							| 
									
										
										
										
											2008-08-29 05:33:05 -04:00
										 |  |  |             dup first-child-tag main>> "fault" =
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |             [ parse-fault <rpc-fault> ] | 
					
						
							|  |  |  |             [ parse-rpc-response <rpc-response> ] if
 | 
					
						
							|  |  |  |         ] [ "Bad main tag name" server-error ] if
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : post-rpc ( rpc url -- rpc )
 | 
					
						
							|  |  |  |     ! This needs to do something in the event of an error | 
					
						
							| 
									
										
										
										
											2008-06-04 20:54:05 -04:00
										 |  |  |     >r send-rpc r> http-post nip string>xml receive-rpc ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : invoke-method ( params method url -- )
 | 
					
						
							|  |  |  |     >r swap <rpc-method> r> post-rpc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : put-http-response ( string -- )
 | 
					
						
							|  |  |  |     "HTTP/1.1 200 OK\nConnection: close\nContent-Length: " write
 | 
					
						
							|  |  |  |     dup length number>string write
 | 
					
						
							|  |  |  |     "\nContent-Type: text/xml\nDate: " write
 | 
					
						
							|  |  |  |     now timestamp>http-string write "\n\n" write
 | 
					
						
							|  |  |  |     write ;
 |