In this chapter, you willieam how ColdFusiori can be used to access data an Web servers using the CFHTIP tag. With th,is tag you can retrieve files from Web servers, store them on the server’s hard disk, and display them in the user’s browser.In addition, it is possible to put together queries to be posted to a Web server for processin or storage.
Although this seems like an obscure use of ColdFusion-using a Web application written in ColdFusion to access another Web server-there are many applications of this tag. To highlight this, you will develop a simple application: a system that organizations can use to minimize downloading software from the Internet, Users who want to down- .load software from the Internet start the ~wnioad from a ipedaI page that actually downloads the file and saves it and then enables the user to download the file. In the future, all other users simply receive the local copy of the file.
Understanding the CFHTTP Tag
Before diving into the technicalities of the CFHTIP tag, you first need to consIder the major types of actions that can be performed with the tag. The CFHTIP tag can be used for four main tasks:
Retrieving pages from a Web server for local storing or rendering to the user’s browser .
• Converting delimited files into usable queries
• Uploading MIME file types to a Web server
• Sending data to a server for processing
Let’s look at each of these in turn and consider their applications.
Retrieving Pages from a Web Server
This is probably the most mystifying use of all. The template file in which the CFHTIP tag is processed is a Web page, so why would you want to retrieve one as part of a template? After all, it is possible to redirect a user’s browser to a page with the CFLOCATION tag. The heart of the matter lies in the role played by the CFHTIP tag. The CFLOCATION tag . simply sends the user to a new URL, and the template containing the tag ceases processing. In contrast, the CFHTTP tag can be used to access a URL and receive the resulting data, but the template can then continue processing, including manipulating the data received from the Web server.
This is an important distinction. When retrieving pages, several important things can happen: .
• The contents of the pages are stored in variables. The content of variables can then be displayed as part of a template’s output by using the CFOUTPUT tag ‘The contents of retfieved pages can be saved to files on the ColdFusion server for later access and manipulation.
• MIME types can be specified when retrieving files, enabling binary files to be retrieved and then processed asneeded .
The is capabi;lities can be used in several ways, including:
CreatIng Intelligent agents An appliCation can be created in which users can specify pages.they are interested in. Then, a scheduled template can automatically download those pages on a regular basis and save them on the server to improve access times to those pages for-users.
MIrroring flies If your applications rely on files generated on other servers, it is possible to download those files to the ColdFusion server on a regular basis to ensure the current version is always available for processing by your templates
Converting Delimited Text File. into Queries
A common file is the delimited ASCD text file. Files of this type contain multiple rows of fields with each field separated by a specified delimiter (often a comma or a tab character). These files are usually. structured as follows:
Each record is on a separate line with a carriage return marking the end of the record. .
• Each record contains the same number of characters.
• ‘The first line of the file usually contains the names of the fields in each record separated by the delimiter (as opposed to the actual contents of a record).
This format creates what is essentially a simple database. It is so useful for transmittingthis type of tabular data between applications that it is often used as the interchange file format between databases, spreadsheets, and other applications that handle this type of tructured data.
Because it is such a common format, data stored in these types of files often can be used by ColdFusion teinplates as query data. For example, if you have a database server that regularly generates a report in delimited text format, you can use the CFHlTP tag to retrieve the file, break it down by record and field, and then provide the result in a query . ‘tblt can then be p~ like all other queries (for instance, by using the CFOUTPUTand OOPtags).
Uploading Binary File.
AI you leamed in Chapter 18, “File Management,” you can use ColdFusion to process and store files that are uploaded from a formby using a file upload field. However, it can be useful to upload files to a Web server without requiring the user interaction that is essential in the form-based method. Let’s consider an example: An
organization has an application that runs on an internal Web server sitting behind the company’s firewall. One of the actions that this application performs is to generate a set of HTML files based orrthe current state of data in a database. These files, however, must be accessible to the general public. There are several ways you can achieve this without the CF~TI? tag, but each has drawbacks:
• Allow outside users access through the firewall to the server running the application, effectively negating the role of the firewall in securing the internal network.
• Manually copy the files to a public Web server that sits outside the firewall. However, if the files are needed frequently or the number of files is large, this can be a tedious and even difficult process.
• Automate the copying of the files with scripts that run on a regular basis. This poses two problems, however. First, depending on the scripting language used, writing the script can prove difficult. Second, if the application does not generate the HTML files on a set schedule, but rather in response to certain actions, a delay can occur before the scheduled script runs to copy the files to the public Web server.
The CFHTIP tag, then, could be used to allow the files to be uploaded to the public Web server right after the ColdFusion application finishes creating them. This minimizes the need for human involvement and ensures that there are no scheduling conflicts or issues in the operation.
Sending Data to a Server for Processing
Because Web servers are often used to host data processing applications-in the form of Gl scripts or ColdFusion templates, for example=enotljer use of the CFHTIP tag is to access these applications and at the same time provide the necessary data for them to perform their assigned tasks.
By combining the CFHTIP tag with the CFHTIPPARAM tag, you can define data to be sent to the server (the role of the CFHTIPPARAM tag) including cookies, form data, and CGI, URL, and file variables, and then have them all sent to the specified Web-server-based application with the CFHTIP tag.