HTTP and Server-Side Processing

Peter Wood

HTTP/1.0 Overview

HTTP Interaction

HTTP/1.1 Persistent Connections

HTTP persistent connections


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 80
Connected to
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" "">
<html xmlns="" xml:lang="en" lang="en">
<title>Computer Science and Information Systems Birkbeck University of London</title>



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=""
  <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" />
  • 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: 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

<head><title>WWW Inventor</title></head>
if ($_GET['inventor'] == "Berners-Lee")
   echo "Congratulations, you answered correctly.";
   echo "Sorry, the correct answer is Berners-Lee.";
  • 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:
  • 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">
        <xsl:for-each select="acronym">
            <xsl:value-of select="."/>

Processing XML with PHP (2)

  • PHP code in acronyms.php is:
    $xmlDoc = new DomDocument();
    $xslDoc = new DomDocument();
    $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
  • they also provide an API for retrieving information about prizes in JSON
  • e.g., to retrieve the winners from 1991, use the URL
  • 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


  • 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


  • 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

PHP code for Nobel Prizes (1)

<h1>Nobel Prize Winners</h1>
  $year = $_GET['year'];
  if ($year >= 1901 && $year <= 2017) {
    $url = '' . $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>";
  • 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):

     "surname":"de Gennes",
     "firstname":"Richard R.",
     "firstname":"Aung San Suu Kyi",
     "firstname":"Ronald H.",

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