Docsity

Introduction to the Web Serving Programming | CSCI 553, Study notes of Computer Systems Networking and Telecommunications

Partial preview of the text

Download Introduction to the Web Serving Programming | CSCI 553 and more Study notes Computer Systems Networking and Telecommunications in PDF only on Docsity!

CSCI 553: Networking III

Unix Network Programming

Spring 2007

Web Server Programming

Introduction

Most of the web's power comes from the

fact that browsers can interact with

programs

More accurately, browsers can ask web servers

to run programs on their behalf

This lecture looks at what to do if you

receive an HTTP request

Very important that you go through the lecture

on security before putting your programs on the

web

The CGI Protocol

The Common Gateway Interface (CGI) protocol

specifies:

 How a web server passes information to a program  How that program passes information back to the web server 

CGI does not specify:

 A particular language  (^) You can use Fortran, the shell, C, Java, Perl, Python…  How the web server figures out what program to run  (^) Each web server has its own rules  (^) We'll (briefly) talk about Apache's

From Server to CGI

Web server runs the CGI by creating a new

process

Figure 14.1: CGI Data Processing Cycle

From CGI to Server

The CGI program sends data back to the web

server by printing it to standard output

The web server then forwards this directly to the

client

 (^) Which means that the CGI program is responsible for creating headers 

Note: none of this works unless the web server

has been configured to run the CGI

 By default, modern servers won't do this unless they're told they can

MIME Types

 (^) Clients and servers need a way to specify data types to each other  (^) Remember, bytes are just bytes: the browser doesn't magically know how to interpret them

 Multipurpose Internet Mail Extensions standard specifies how to do this

 (^) Organizes data types into families, and provides a two-part name for each type  (^) Use the "Content-Type" header to specify the MIME type of the data being sent application/pdf Adobe PDF document Application-specific data Apple Quicktime video format Video^ video/quicktime Audio audio/x-mp3 MP3 audio file Image image/jpeg JPEG-format image Text text/html Web pages Family Specific Type Describes

Invoking a CGI

 (^) Invoke it by going to http://www.yourserver.com/cgi- bin/hello_cgi.py  (^) By convention, CGI programs are put in a cgi-bin directory  (^) Browser displays the simple HTML page generated by the program Figure 14.2: Basic CGI Output

Generating Dynamic Content

 (^) But the whole point of CGI is to generate content dynamically  (^) E.g., show a list of environment variables and their values #!/usr/bin/env python import os, cgi

Headers and an extra blank line

print 'Content-type: text/html' print

Body

print '' keys = os.environ.keys() keys.sort() for k in keys: print '

%s: %s

' % (cgi.escape(k), cgi.escape(os.environ[k])) print ''

Forms

Next step is to allow users to enter data

Without manually editing URLs to append

parameters

HTML forms allow users to enter text,

choose items from lists, etc.

Not nearly as sophisticated as desktop

interfaces

Although programmers are doing more every

day (particularly using Javascript)

Creating Forms

 Create a form using a … element  (^) action attribute specifies the URL to send data to  (^) method attribute specifies the type of HTTP request to send  (^) Usually "POST" for HTML forms  (^) Inside the form, can have:  (^) elements to let users choose values from a list  (^) List items specified using elements for other kind of data  (^) If type is "text", get a one-line text entry box  (^) If type is "checkbox", get an on/off checkbox  (^) "submit" and "reset" create buttons to submit the form, or re-set the data to initial values

Parameter Names

 Each element has a name attribute  (^) These become the names of the parameters that the client sends to the server  (^) The input elements' values are the parameters' values  Submitting the form shown above with default values produces:  (^) os.environ['REQUEST_METHOD']: "POST"  (^) os.environ['SCRIPT_NAME']: "/cgi-bin/simple_form.py"  (^) os.environ['CONTENT_TYPE']: "application/x-www-form- urlencoded"  (^) os.environ['REQUEST_LENGTH']: "80"  (^) Standard input: sequence=GATTACA&search_type=Similarity +match&program=FROG-11&program=Bayes-Hart

Handling Forms

Could handle form data directly

 (^) Read and parse environment variables  (^) Read extra data from standard input  (^) But the mechanics are the same each time, so use Python's cgi module instead  (^) Defines a dictionary-like object called FieldStorage  (^) Keys are parameter names  (^) Values are either strings (if there's a single value assocatied with the parameter) or lists (if there are many)  (^) When a FieldStorage object is created, it reads and stores information contained in the URL and environment  (^) Which means that a CGI program should only ever create one  (^) Program can read extra data from sys.stdin

Development Tips

 (^) During development, add import cgitb; cgitb.enable() to the top of the program  (^) cgitb is the CGI traceback module  (^) When enabled, it will create a web page showing a stack trace when something goes wrong in your script  Testing whether a FieldStorage value is a string or a list is tedious  (^) In almost all cases, you'll know whether to expect one value or many  (^) Use FieldStorage.getfirst(name) to get the unique value  (^) Returns the first, if there are many  (^) FieldStorage.getlist(name) always returns a list of values  (^) Empty list if there's no data associated with name  (^) If there's only one value, get a single-item list

Maintaining State

 (^) Often want to change the data a server is managing, as well as read it  (^) Update a description of an experiment, change your preferred email address, etc.

 The industrial-strength solution is to use a three-tier architecture

 (^) CGI program stuffs parameters from HTTP requests into SQL queries  (^) Runs the queries  (^) Translates results into HTML to send back to the client Figure 14.5: Three Tier Architecture