HTTP and Server-Side Processing

Peter Wood

HTTP/1.0 Overview

HTTP Interaction

HTTP/1.1 Persistent Connections

HTTP persistent connections

Pipelining

HTTP pipelining

HTTP Client Requests

HTTP Server Responses

Some request and response headers

  • form of each HTTP header is field: value
  • (some) client request headers
    • Host: the domain name (and port) of the server; required in every request; allows server to differentiate requests for multiple hosts with same IP address
    • User-Agent: information about the client program (type, version)
    • Accept: formats acceptable to the client, given using MIME types
  • (some) server response headers
    • Server: name and version of server
    • Content-Type: the (MIME) media type of the resource being returned
    • Content-Length: size of message body in bytes
    • Last-Modified: date and time when entity was last modified

Example of request and response

Machine responses are this colour below:

Peter-Woods-MacBook-Pro:~ ptw$ telnet www.dcs.bbk.ac.uk 80
Trying 193.61.29.21...
Connected to www.dcs.bbk.ac.uk.
Escape character is '^]'.
GET / HTTP/1.0

HTTP/1.1 200 OK
Date: Fri, 18 Nov 2011 17:44:06 GMT
Server: Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8o DAV/2 SVN/1.6.5 mod_fcgid/2.3.6 mod_perl/2.0.4 Perl/v5.8.4
Connection: close
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Computer Science and Information Systems Birkbeck University of London</title>

...

</body>
</html>

Connection closed by foreign host.
Peter-Woods-MacBook-Pro:~ ptw$

Example using HTML form

  • servers don't only deliver (static) documents
  • they can also run programs to generate content
  • usually based on user input via an HTML form, e.g.:

HTML for form

<form action="http://titan.dcs.bbk.ac.uk/~ptw/teaching/IWT/server/www-inventor.pl"
      method="GET">
  <label for="www-inventor">Who invented the WWW (surname only)?</label>
  <input type="text" id="www-inventor" name="inventor" />
  <input type="submit" value="Submit answer" />
  <input type="reset" value="Erase answer" />
</form>
  • action gives URI of Perl script
  • method used is GET
  • only one input control with name attribute
  • so ?inventor=Berners-Lee, e.g., added to URL-path
  • communication between web server and Perl interpreter uses Common Gateway Inteface (see next slide)
  • query string is stored in QUERY_STRING environment variable

Common gateway interface (CGI)

  • CGI was first consistent server-independent mechanism for server-side processing
  • CGI defines an interface between servers and external programs
  • server and CGI application run on the same machine (as separate processes)
  • CGI is language-neutral
  • CGI applications (or scripts) can be implemented in any language supporting inter-process communication, e.g.,
    • Perl, python, tcl, Visual Basic, C/C++, ...
  • script is executed when client asks to GET script's URI
  • script needs to generate entire HTML page

HTML forms and CGI

  • HTML form element has
    • action attribute whose value is the URI for processing agent
    • method attribute whose value specifies the HTTP method to use for sending data: GET or POST

    form handling with CGI

  • form input data is sent to the server as name=value pairs
    • name is the value of the name attribute of an input control
    • value is the value of the corresponding value attribute or is entered by the user
  • name=value pairs are separated by & characters
  • for GET, name=value pairs form the URI query string
  • for POST, name=value pairs form the message body

Perl code for www-inventor.pl

#!/usr/bin/perl
print "Content-type: text/html", "\n\n";
$query_string = $ENV{'QUERY_STRING'};
($question, $answer) = split (/=/, $query_string);
print "<html>", "\n";
print "<head><title>WWW Inventor</title></head>", "\n";
print "<body>", "\n";
if ($answer eq "Berners-Lee") {
   print "Congratulations, you answered correctly.";
} else {
   print "Sorry, the correct answer is Berners-Lee.";
}
print "</body></html>", "\n";
exit (0);
  • location of the Perl interpreter is /usr/bin/perl
  • response must contain at least the Content-type header
  • output is passed to the server via standard output
  • variable query_string gets the value of the QUERY_STRING environment variable
  • query_string is split into 2 strings: that before the = and that after it
  • variable question gets the value inventor
  • variable answer gets the value of what user typed in

Server-side processing technologies

  • common gateway interface (CGI) programs (already covered)
  • Java servlets
  • server-side include (SSI) pages
  • Java server pages (JSP)
  • active server pages .NET (ASP.NET)
  • PHP
  • ...

PHP

  • PHP: Hypertext Preprocessor (PHP) is an open-source, server-side scripting language
  • originally based on Perl scripts
  • PHP 5 provides an object-oriented approach
  • variables don't have to be declared and are not strongly typed
  • uses <?php and ?> delimiters for PHP code
  • delimiters differentiate PHP code from static HTML
  • server needs to have PHP installed

PHP code for www-inventor.php

<html>
<head><title>WWW Inventor</title></head>
<body>
<h1>
<?php
if ($_GET['inventor'] == "Berners-Lee")
   echo "Congratulations, you answered correctly.";
else
   echo "Sorry, the correct answer is Berners-Lee.";
?>
</h1>
</body>
</html>
  • variable names start with $
  • built-in variable $_GET is an associative array providing access to the values of name=value pairs in the query string
  • so $_GET['inventor'] gets value of inventor parameter in query string
  • output passed to server via standard output - using echo

Processing XML with PHP (1)

  • very simple application: an XML file containing acronyms:
    <dictionary>
      <acronym>AJAX</acronym>
        ...
      <acronym>XSLT</acronym>
    </dictionary>
    
  • want to transform the XML on the server using a stylesheet and send the resulting HTML output to the browser
  • stylesheet produces an unordered list with each list item containing an acronym:
    <xsl:template match="/dictionary">
      ...
      <ul>
        <xsl:for-each select="acronym">
          <li>
            <xsl:value-of select="."/>
          </li>
        </xsl:for-each
      </ul>
      ...
    </xsl:template>
    

Processing XML with PHP (2)

  • PHP code in acronyms.php is:
    <?php
    $xmlDoc = new DomDocument();
    $xmlDoc->load("/home/ptw/public_www/teaching/IWT/server/acronyms.xml");
    
    $xslDoc = new DomDocument();
    $xslDoc->load("/home/ptw/public_www/teaching/IWT/server/acronyms.xsl");
    
    $processor = new XSLTProcessor();
    $xslDoc = $processor->importStylesheet($xslDoc);
    
    $htmlDoc = $processor->transformToDoc($xmlDoc);
    print $htmlDoc->saveXML();
    ?>
    
  • DomDocument and XSLTProcessor are PHP objects
  • load, importStylesheet and transformToDoc are PHP methods
  • note the similarity to client-side Javascript code
  • the saveXML method converts a DOM tree to a string
  • full path names for files should not be needed

Modifying server-based information

  • say we want to allow a user to add new acronyms to the XML file acronyms.xml
  • we can use an HTML form, e.g.
  • the HTML for the form is
    <form action="http://www.dcs.bbk.ac.uk/~ptw/teaching/IWT/server/addAcronymAction.php"
          method="GET" >
      <input name="acronym" type="text" size="30"/>
      <input type="submit" value="Add Acronym"/>
    </form>
    

addAcronymAction.php

<html>
  <head/>
    <body>
<?php
  $acronym = $_GET['acronym'];

  $xmlDoc = new DomDocument();
  $xmlDoc->load("/home/ptw/public_www/teaching/IWT/server/acronyms.xml");

  $newAcronymElement = $xmlDoc->createElement("acronym");
  $newAcronymElement->appendChild($xmlDoc->createTextNode($acronym));

  $root = $xmlDoc->documentElement;
  $root->appendChild($newAcronymElement);

  if ($xmlDoc->save("/home/ptw/public_www/teaching/IWT/server/acronyms.xml"))
    print "<p>You have successfully updated the XML document</p>";
  else
    print "<p>An error occurred</p>";
?>
    </body>
</html>

addAcronymAction.php explanation

  • createElement, createTextNode and appendChild are analogous to the corresponding client-side DOM methods
  • documentElement is analogous to the corresponding DOM property
  • the save method writes the DOM tree as a string to a file (the same as the input file)
  • file permissions must be set to allow writing to the file
  • an HTML message is displayed indicating whether the script executed successfully or not

Links to more information

HTTP is covered briefly in Section 4.6 of [Comer]. Section 7.3 of [Tanenbaum] covers HTML forms and HTTP, and mentions CGI and PHP. Servlets are covered in Chapter 9 and JSP in Chapter 10 of [Moller and Schwartzbach]. Server-side processing of XML using PHP (and VB.NET) is covered in Chapters 11 (12) and 13 in [Jacobs].