FastCGI for Desktop Applications


Published on

The web is playing an increasingly important role for desktop software. Developers often need to interact with a web server for tasks like; installation, authentication, application updates, data transfer, error reporting and secure transactions for billing. Typically these applications use HTTP and port 80 to avoid firewall support issues.

FastCGI provides a standard protocol for many servers including Windows IIS and Linux Apache, and most web development languages like php, Java, Perl, Ruby, Python etc for developing applications that execute on the server side. However, FastCGI applications may also be written in the same powerful high level languages (Basic/C/C++) used to create the desktop software, making code instantly reusable on the server side.

While Java/Javascript and .NET tend to dominate the web capable application development space these days, both have disadvantages for high performance desktop software. As an interpreted language with issues, JavaScript is not ideal, and .NET's 'managed code' and reliance on COM to wrap API's often leads to speed and reliability issues. FastCGI provides a powerful alternative protocol, supported by most servers and languages, that integrates into a development project quickly and easily.

The presentation will touch on the HTTP protocol in general, the WinHTTP library and reasons for choosing the flat API over the COM interface, the concept of the FastCGI protocol, FastCGI vs CGI and how it has evolved recently. I will then show you how to write, deploy and test a FastCGI application on your laptop in two minutes using a powerful free web server - see my Blog for more details

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

FastCGI for Desktop Applications

  1. 1. FastCGIfor Desktop Applications<br />
  2. 2. Development Considerations<br />Fast to develop<br />Same language <br />Same code at each end<br />Reliable<br />Scalable<br />Zero Configuration (Ports, Firewalls etc)<br />No additional support load<br />
  3. 3. Windows Libraries<br />1 Application Side – HTTP -&gt;(TCP/IP)<br />(TCP is optimized for non-real-time applications such as file transfer)<br /><ul><li>- WinINet
  4. 4. - WinHTTP</li></ul>2 Server Side: (IIS)<br /><ul><li>- ISAPI
  5. 5. - CGI
  6. 6. - FastCGI</li></li></ul><li>WinHTTP<br /><ul><li>Designed to provide client side HTTP services
  7. 7. More secure and robust than WinINet
  8. 8. Fewer dependencies on platform-related APIs
  9. 9. Supports HTTP/1.1 chunked transfer support for HTTP responses.
  10. 10. Geared towards a non-interactive environment
  11. 11. Called with the Win32 API set or a COM interface
  12. 12. Secure Sockets Layer (SSL) functionality
  13. 13. Unique Error codes</li></ul>WinINet<br /><ul><li>Legacy library
  14. 14. Designed for interactive message dialogs
  15. 15. Supports FTP, URL caching and persistent cookies</li></ul>1. WinInet Vs WinHTTP<br />
  16. 16. <ul><li>FastCGI is not proprietary
  17. 17. FastCGI is portable across multiple languages, with zero code changes
  18. 18. A FastCGI app is an isolated process (no web server reset)
  19. 19. A Poorly written FastCGI app won't crash the web server.
  20. 20. FastCGI app servers can run on different hosts from the web server (allows for extremely high performance, distributed web sites.)
  21. 21. FastCGI app servers can run on many OS's
  22. 22. FastCGI is Mature
  23. 23. FastCGI on IIS is actively developed and well supported by Microsoft</li></ul>ISAPI vs CGI/FastCGI<br />
  24. 24. <ul><li>FastCGIvs CGI (powerpoint)
  25. 25. Persistence – Retains Context
  26. 26. Now with SIGTERM Handler (pipes)
  27. 27. FastCGI will outperform standard CGI
  28. 28. (by a factor of 4:1 ~200 rps vs. ~50 rps)
  29. 29. You don't lose your context:
  30. 30. Allows efficient caching,
  31. 31. Share results between requests</li></ul>FastCGI<br />
  32. 32. The main task of converting a CGI program into<br />FastCGI program is separating the initialization <br />code from the code that needs to run for each request.<br />The structure should look something like this:<br /><ul><li>- Initialization code
  33. 33. - Start of response loop</li></ul>&apos;<br />&apos; body code<br />&apos;<br /><ul><li>- End of response loop</li></ul>Writing a FastCGI Application<br />
  34. 34. <ul><li>FCGX_InitRequest()
  35. 35. Initializes a FastCGI instance
  36. 36. FCGX_Accept_r()
  37. 37. Blocks until a new FastCGI request is received
  38. 38. FCGX_PutStr()
  39. 39. Return a valid HTTP header
  40. 40. Return nBytes of text/data</li></ul>FastCGI Library Functions:<br />
  41. 41. Hello World in BASIC<br />#INCLUDE &quot;FCGX_Header.bas&quot; <br />FUNCTION WINMAIN<br /> LOCAL Count as LONG<br /> LOCAL sReply AS STRING <br /> LOCAL FCGXReq AS FCGX_REQUEST &apos;// FCGX Structure<br /> &apos;// Open Databases<br /> CALL FCGX_InitRequest(FCGXReq, 0, 0) <br /> DO &apos;// Main Request processing loop<br /> INCR Count<br /> IF FCGX_Accept_r(FCGXReq) &lt; 0 THEN EXIT LOOP &apos;//Execution blocked until HTTP request arrives<br />sReply = &quot;Content-Type: text/html&quot;+$CRLF+$CRLF &apos;// The reply begins with a valid HTTP header <br />sReply = sReply + “Hello World, Count=&quot; + STR$(Count) <br />FCGX_PutStr( STRPTR(sReply), LEN(sReply), FCGXReq.pOut)<br /> LOOP <br /> &apos;// Close Databases etc here <br />END FUNCTION <br />
  42. 42. Hello World in C++<br />#include &lt;libfcgi2.h&gt; <br />int main( ) { <br />req FCGX_REQUEST;<br />int count(0);<br /> string sReply ;<br />ostringstreamss;<br /> // Open Database<br />FCGX_InitRequest( req, 0, 0 );<br /> while (true) {<br /> if( FCGX_Accept_r(req) &lt; 0 ) break; // Execution is blocked until a Request is received<br />count++; <br />ss &lt;&lt; count; // Typesafe conversion to text<br />sReply = &quot;Content-Type: text/html Hello World &quot; + ss.str();<br />FCGX_PutStr(, sReply.size(), req-&gt;pOut) ; <br /> }<br /> // Close Database<br />return 0;<br />}<br />
  43. 43. <ul><li>Completely new library
  44. 44. Gracefull exit – close databases etc
  45. 45. The 'SignalBeforeTerminateSeconds' property in fcgiext.ini determins timing of the SIGTERM notification event to be sent by the Web Server to this library under IIS
  46. 46. Request Method is available as an int(4 byte signed integer)
  47. 47. CONTENT_LENGTH is available as an int(4 byte signed integer)
  48. 48. GET method Query String is directly available
  49. 49. Open access to input/output buffers</li></ul>Libfcgi2 - SIGTERM<br />
  50. 50. SQL requests can generate large responses!<br />XML introduces severe bloat<br />An example framework for interacting with a <br />database on a server <br />- Chunks<br />- Encryption/Compression<br />- Binary Data<br />Chunk Protocol:<br />HEADER<br />DATA BODY<br />STRINGS<br />Remote SQL<br />
  51. 51. Request/Response Model:<br />Authorization<br />Feature control<br />Billing<br />Updating<br />Data transfer<br />Pushing marketing content<br />Deter Piracy<br />Example Application<br />
  52. 52.<br /><br /><br /><br />WebSites<br />