FastCGI for Desktop Applications
Upcoming SlideShare
Loading in...5
×
 

FastCGI for Desktop Applications

on

  • 3,393 views

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

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 http://mbbz.blogspot.com

Statistics

Views

Total Views
3,393
Views on SlideShare
3,385
Embed Views
8

Actions

Likes
0
Downloads
10
Comments
0

1 Embed 8

http://www.slideshare.net 8

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

FastCGI for Desktop Applications FastCGI for Desktop Applications Presentation Transcript

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