21.8 Receiving Streaming Audio Through a Browser

Team-FLY

This section discusses how to run the Internet Radio programs from a browser. Create a web page containing a list of links to the broadcasts that are available. When a user clicks on a link, the browser launches a receiver helper program to receive and play the audio program.

21.8.1 Using browser helper applications

You may have noticed that when you click on certain links, the corresponding file does not appear in your browser window, but rather the browser launches a separate program, called a helper application , to handle the data sent by the server. For example, if you have a Real Audio Player installed on your machine and have set your browser to use this application, clicking on a link for a file with extension ram causes the browser to store the corresponding file as a temporary file on the local machine. The browser then launches the Real Audio Player application, passing the temporary file name to the application as a command-line argument. The file contains the information the Real Audio Player needs to locate the audio program.

Browsers use one of two methods to identify the type of resource being sent and the application that should handle this resource. Some browsers use the file extension to determine the type of resource; others rely on a Content-Type header line in the server response. Browsers that use file extensions store the correspondence between resource types and filename extensions in a file, typically named mime.types . The word MIME is an acronym for Multipurpose Internet Mail Extensions and was originally intended for mail attachments. Applications now interpret mime types more generally to associate an application type with a file extension. Web server responses often include a header line that describes the type of resource being sent.

Exercise 21.49

For an ordinary text document in HTML format, a server might send the following header line.

 Content-Type: text/html 

For a file with the ram extension, the server might send the following.

 Content-Type: audio/x-pn-realaudio 

When the browser receives this header line, it checks to see if a helper application has been set up with type audio/x-pn-realaudio , and if so, it puts the resource sent by the web server in a temporary file and calls that application with the name of the temporary file as a command-line argument.

When classifying resources on the basis of file extensions, the browser looks for an entry in its mime.types file corresponding to the ram extension such as the following.

 audio/x-pn-realaudio   ram rm 

The preceding command specifies that both the ram and rm extensions should be associated with audio applications of type x-pn-realaudio .

Start with one of your receiver programs, say, TCPRecv.c from Section 21.7.1 and copy it into TCPRecvMime.c . Modify TCPRecvMime to take one command-line argument, the name of a file containing the host name and port number of the sender.

Exercise 21.50

Suppose TCPRecvMime uses the following to read the host name and port tokens from the file specified on its command line.

 scanf("%s %d", hostname, &port); 

What problems might occur, assuming that hostname is an array of char and that port is an integer?

Answer:

The TCPRecvMime program has no way of telling in advance how long the host name is. Although valid host names cannot be too long, anything can appear in the resource file referenced on a web page. A bad resource file could generate a buffer overflow with potentially serious security implications. One solution is to allocate a buffer of prespecified size , say, 80 bytes, for the host name and use the following line.

 scanf("%79s %d", hostname, &port); 

The numerical qualifier on %s prevents scanf from filling hostname with more than 79 characters and the string terminator.

Test TCPRecvMime with TCPSend by creating a file containing the host and port number. Setting TCPRecvMime to be launched through a browser requires the following three steps that are described in the subsections below.

  1. Set the web server to handle a new mime type and send the appropriate Content-Type line. (This step needs a system administrator and is necessary for browsers that use this line to determine the application type.)

  2. Set your browser to handle the new mime type by launching TCPRecvMime when it receives a resource of the appropriate type.

  3. Create a web page for testing.

21.8.2 Setting a new mime type in your web server

Setting up your web server to handle a new mime type requires that you have administrative access to the web server. If you do not have administrative access, ask your system administrator to do this step for you. Alternatively, you can use one of the mime types already set up for your browser. We discuss this option in Section 21.8.5.

Depending on your web server, you can set a new server mime type by modifying a file of mime types or by modifying the configuration file. For example, if your web server configuration directory has a file with a name similar to mime.types , add the following line to this file.

 application/uspir      uspir 

The preceding line allows the web server to associate an application type called application/uspir with the file extension .uspir . Alternatively, you might be able to just add the following line to the web server configuration file, possibly a file called httpd.conf .

 AddType application/uspir      uspir 

You must restart the web server after changing this file.

You can use the client2 program from Program 18.5 on page 629 to verify that your web server is set correctly for this mime type. Create a small file called test.uspir in a directory accessible to the web server. If the web server is running on host webhost and this file is in the directory mydir relative to the web root directory, start client2 with the following command.

 client2 webhost 80 

Type the following line terminated by an empty line.

 GET /mydir/test.uspir HTTP/1.0 

You should see the file after a few header lines. A correct response should have a header line similar to the following.

 Content-Type: application/uspir 

21.8.3 Setting your browser to handle a new mime type

The method for setting a new mime type for a browser depends on which browser you are using. For Netscape 6 or 7, go to Edit Preferences Navigator Helper Applications. Click on New Type and fill in the information requested . The Description can be any phrase. The File extension should be uspir and the MIME type should be application/uspir . For the application, put the full pathname for your TCPRecvMime program.

21.8.4 Creating a web page

Create a file with extension .uspir containing the host name and port number of your TCPSend program. The values should specify a server that is distinct from the web server. Make the file accessible to your web server and create a web link to the file. Start TCPSend . When you click on the link, you should start hearing the audio program.

21.8.5 Using a predefined mime type

If you cannot add a new mime type to your web server, you can use one of the predefined types that your browser is not using or does not use often. Some suggested extensions to try are ez , hqx , cpt , oda , smi and mif . You can test these by creating a file with the appropriate extension in a place accessible to your web server and issuing the appropriate GET command from client2 . You should get back a Content-Type line giving the corresponding application type.

Set your browser to call your TCPRecvMime program for this application type. Follow the procedure in Section 21.8.3. If the application type is already defined for your web browser, click EDIT and modify the values.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net