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.php"
      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 PHP script (see later) to run
  • method used is GET
  • only one input control with name attribute
  • so ?inventor=Berners-Lee, e.g., is added to URL-path

HTML form processing

  • 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 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

Server-side processing technologies

  • common gateway interface (CGI) programs (very old)
  • 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
  • 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

Retrieving JSON

  • information about Nobel prizes is available at https://www.nobelprize.org
  • they also provide an API for retrieving information about prizes in JSON
  • e.g., to retrieve the winners from 1991, use the URL
    http://api.nobelprize.org/v1/prize.json?year=1991
  • query parameters include
    • year: year in which prizes were awarded
    • yearTo: ending year for a range of years (year required)
    • category: one of the 6 categories
    • numberOfLaureates: filter prizes by number of winners sharing the prize

REST

  • the Nobel prize API is what is called a REST API
  • REST (REpresentational State Transfer), or RESTful, web services provide
    • access to and manipulation of web resources
    • using a uniform and predefined set of stateless operations
    • when using HTTP, these operations include GET, POST, PUT, DELETE
    • all information is encoded in the URLs and resources returned
  • typically
    • a URI identifies a resource
    • GET will retrieve a resource
    • POST will create a new resource
    • PUT will overwrite an existing resource
    • DELETE will delete a resource

PHP and JSON

  • let's use a simple form to pass a year value to the Nobel prize API
  • it is sent via a PHP script (next slide) which also turns the JSON response into HTML
  • the name of the textbox in the form is year
  • the action value is http://titan.dcs.bbk.ac.uk/~ptw/teaching/IWT/server/nobel-year.php

PHP code for Nobel Prizes (1)

<html>
<body>
<h1>Nobel Prize Winners</h1>
<?php
  $year = $_GET['year'];
  if ($year >= 1901 && $year <= 2017) {
    $url = 'http://api.nobelprize.org/v1/prize.json?year=' . $year;
    $string = file_get_contents($url);

    # Read the JSON output into an associative array
    $result  = json_decode($string, true);

    print "<p>In $year, the prizes were awarded as follows:</p><ul>\n";
     ...  # see next slide
    print "</ul>";
  }
  else {
    print "<p>Year value out of range; years range from 1901 to 2017</p>";
  }
?>
</body>
</html>
  • we previously used $_GET (on slide 15)
  • string concatenation in PHP uses .
  • file_get_contents returns file contents as a string
  • json_decode with second parameter set to true returns JSON string as an associative array
  • variable references, such as $year, inside strings delimited by double quotes are dereferenced (but not for single quotes)

PHP code for Nobel Prizes (2)

returned JSON data is as follows (for year 1991):

{"prizes":
 [
  {"year":"1991",
   "category":"physics",
   "laureates":[
    {"id":"141",
     "firstname":"Pierre-Gilles",
     "surname":"de Gennes",
     "motivation":"...",
     "share":"1"}]},
  {"year":"1991",
   "category":"chemistry",
   "laureates":[
    {"id":"276",
     "firstname":"Richard R.",
     "surname":"Ernst",
     "motivation":"...",
     "share":"1"}]},
  {"year":"1991",
   "category":"medicine",
   "laureates":[
    {"id":"444",
     "firstname":"Erwin",
     "surname":"Neher",
     "motivation":"...",
     "share":"2"},
    {"id":"445",
     "firstname":"Bert",
     "surname":"Sakmann",
     "motivation":"...",
     "share":"2"}]},
  {"year":"1991",
   "category":"literature",
   "laureates":[
    {"id":"668",
     "firstname":"Nadine",
     "surname":"Gordimer",
     "motivation":"...",
     "share":"1"}]},
  {"year":"1991",
   "category":"peace",
   "laureates":[
    {"id":"553",
     "firstname":"Aung San Suu Kyi",
     "motivation":"...",
     "share":"1",
     "surname":""}]},
  {"year":"1991",
   "category":"economics",
   "laureates":[
    {"id":"707",
     "firstname":"Ronald H.",
     "surname":"Coase",
     "motivation":"...",
     "share":"1"}]}
 ]
}

PHP code for Nobel Prizes (3)

  • recall $result contains the JSON data as an associative array
  • the rest of the PHP code is as follows:
      # Find out how many prizes are listed
      $num_prizes = count($result['prizes']);
    
      for ($i = 0; $i < $num_prizes; $i++) {
    
        # Print out the category
        $cat = $result['prizes'][$i]['category'];
        print "<li>in $cat to <ul>\n";
    
        # Find out how many winners in this category
        $num_winners = count($result['prizes'][$i]['laureates']);
    
        for ($j = 0; $j < $num_winners; $j++) {
    
          # Print out the names
          $firstname = $result['prizes'][$i]['laureates'][$j]['firstname'];
          $surname = $result['prizes'][$i]['laureates'][$j]['surname'];
          print "<li>$firstname $surname </li>\n";
    
        }
        print "</ul></li>\n";
      }
    
  • count counts number of elements in an array

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].