Using CGI in the LabVIEW Internet Toolkit
Overview
CGI is a standard interface for external gateway programs to communicate with information servers, such as HTTP servers. The LabVIEW Internet Toolkit includes CGI VIs that you can use to build and execute CGI applications, to publish front panel images to the Web, and to maintain client-state information with HTTP cookies. You also can use Web services in LabVIEW to build and execute CGI applications. Refer to the LabVIEW Help for more information about Web services in LabVIEW.
Note: This document includes information that previously existed in the LabVIEW Internet Toolkit User Guide, which was deprecated with the LabVIEW Internet Toolkit 6.0.2 release.
On the Web, when a client sends a request whose URL specifies a CGI application, the server decodes the request, loads the application, and executes the application. The application generates data and returns it to the server. The server then sends a reply that contains this data to the client, which displays the reply.
You can generate documents dynamically using CGI applications. This process can help you when the data in documents changes over time or when you generate the document according to user-supplied criteria.
You can supply parameters for CGI applications in the following ways, depending on the method that an HTML document uses to invoke a CGI application.
- POST method—Use only with HTML forms in which a user clicks a Submit button.
- GET method—Use with HTML links and some HTML forms.
When a user completes an HTML POST form and clicks the Submit button, the Web browser encodes the contents of the fields into an ampersand (&)-separated list of name=value parameter pairs and sends this string to the CGI application as the content of the POST request. For example, if the form contains the fields name and age and the user enters John Smith and 27, respectively, the Web browser sends the following string:
name=John%20Smith&age=27
The string %20 in the name represents a space because 20 is the hexadecimal ASCII value of the space character.
When a user completes an HTML GET form and clicks a button or link, the Web browser encodes the contents into a question mark (?)-separated string and adds the string to the end of the CGI name. An HTML link with parameters explicitly specified usually invokes a CGI application through the GET method. For example, the following URL connects to the server some.server.adr, invokes the CGI application located in /cgi-bin/example.vi, and sends the single parameter parameter.
<http://some.server.adr/cgi-bin/example.vi?single%20parameter>
Similarly, the following URL invokes the CGI application with two parameters name and age with values John Smith and 27, respectively.
<http://some.server.adr/cgi-bin/example.vi?name=John%20Smith&age=27>
Maintaining Client State Information with CGI VIs
HTTP is a stateless protocol. Each time a client wants a document from a server, the client must establish a new connection and send a request. The server receives the request, returns a reply, and closes the connection. The server does not maintain state information between individual connections.
You have several options for maintaining client state information across multiple connections. For example, you can insert information you collect into hidden fields of an HTML form, or you can use a cookie.
A cookie is a name-value pair that corresponds to information an HTTP server stores. Cookies can include information such as user preferences, login identification, and online purchasing information. Use cookies with HTTP servers to customize information that users receive and to keep track of files that users access on a Web site. You can use cookies to maintain information on the client side or on the server side.
CGI VIs store cookies as keyed arrays of name-value pairs: the name of the cookie, such as userID, and the cookie value, a number that the HTTP server uses to identify the information.
Using Client-Side Cookies
You can use client-side cookies to store state information on a client system. Client-side cookies are easy to use and persist even when the server is not running. However, not all browsers work with client-side cookies. Some users do not want servers to write information to their computers without their knowledge, and potential security risks can arise when hosts other than the one that stores the information can access client state information.
You can use the CGI Get Query Client Side Cookies VI, CGI Set Client Side Cookie VI, and CGI Set Multiple Client Side Cookies VI to read and specify client-side cookies. The Internet Toolkit's G Web Server encodes in URL format the name and contents of each cookie that it specifies and decodes from URL format the name and contents of cookies it receives from a Web browser. Refer to the LabVIEW Help for more information about CGI VIs related to client-side cookies.
Using Server-Side Cookies
You can use server-side cookies to store state information about the server. Server-side cookies do not rely on browser software to work correctly, do not store data on client computers, do not transmit as much data as client-side cookies, and have fewer security risks because the server maintains security. However, servers maintain state information only for a certain time that the server specifies, after which the cookie automatically expires.
The G Web Server defines a server-side cookie as a cluster of two strings, cookie ID and address. The server uses the cookie ID string to identify a specific cookie. You can embed a cookie ID in the HTML document that a CGI application creates. The address string ensures that only clients with the same address can access a server-side cookie. If a specific client request creates a cookie, only that client can view and modify the cookie.
You can use the CGI Cookie VI, CGI Spool Cookie VI, CGI Add Params To Cookie VI, and CGI Build Cookie Document VI to work with server-side cookies in HTML documents. You can create, destroy, and modify server-side cookies and add and query information associated with cookies. You also can create HTTP connection-based cookies and documents that contain cookies. Refer to the LabVIEW Help for more information about CGI VIs related to server-side cookies.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
