Sunday, June 04, 2006

Digging into SOAP and WebSnap

In previous articles on this blog we have dissected the internal workings of published methods, and lamented the (assumed) fact that the RTTI for them does not include signature information. We then developed a dirty (and pretty useless) hack to dig out the parameters of a published method by matching it with the RTTI of an event that references the method at runtime. As I briefly mentioned in my last post, David Glassborow pointed me to the extended RTTI that Delphi 6 and later supports.

In Delphi 6, Borland introduced new web services functionality to be able to write SOAP servers and clients. They also released WebSnap – a framework for writing ASP-like web applications with server-side scripting. Both of these frameworks need the functionality to call arbitrary methods with any number and types of parameters dynamically at runtime.

SOAP and method RTTI for interfaces
For SOAP-based Web services, they use invokable interfaces to let a THTTPSoapPascalInvoker component dynamically call methods in a registered interface. An invokable interface is one that inherits from IInvokable, and IInvokable is compiled using the compiler directive $M+ (see System.pas). The compiler generates full RTTI for all the methods in invokable interfaces – including enough parameter information to call the methods dynamically at runtime.

You can read the juicy technical implementation details by browsing the code in IntfInfo.pas, Invoker.pas and RIO.pas. Note that full source for these were not made available in D6 and D7 (there is a in the Doc directory, though). They can be found in the Source\Win32\SOAP directory in D2005 and 2006.

WebSnap and method RTTI for classes
For WebSnap, the “new” $METHODINFO ON compiler directive (which was undocumented in D6 and D7) is used to generate extended RTTI for public and published methods. A class declared in this mode (and all classes that inherit from it) will have RTTI generated for all its public and published methods. In addition to the normal $M+ style published method RTTI with name and address, the extended RTTI for METHODINFO includes the name and type of all parameters, the return type and calling convention. This allows the methods (and published properties) to be accessed from the server-side WebSnap script.

In the WebSnap directory you’ll find the source of the following interesting units ObjAuto, ObjComAuto and WebSnapObjs.

Exploring the fascinating and impressive implementation details in these units is left as an exercise for the reader. We might revisit them for a closer examination in the future.

In coming articles, we’ll look into the details of simple interface RTTI, extended interface RTTI and extended class method RTTI. Stay tuned!

No comments:

Copyright © 2004-2007 by Hallvard Vassbotn