You are on page 1of 34

HyperText Transfer Protocol

HTTP
Outline
• Network apps.
• HTTP overview
• HTTP request
• HTTP response

2
Some network apps
• Web
• email
• text messaging
• remote login
• P2P file sharing
• multi-user network games
• streaming stored video (YouTube, Hulu, Netflix)
• voice over IP (e.g., Skype)
• real-time video conferencing
• social networking
• search

3
Sockets
• process sends/receives messages to/from its socket
• socket analogous to door
– sending process shoves message out door
– sending process relies on transport infrastructure on other
side of door to deliver message to socket at receiving
process

application application
socket controlled by
process process app developer

transport transport
network network controlled
link by OS
link Internet
physical physical

4
Addressing processes
• to receive messages, process must have identifier
• host device has unique 32-bit IP address
• Q: does IP address of host on which process runs
suffice for identifying the process?
– A: no, many processes can be running on same host
– identifier includes both IP address and port numbers
associated with process on host.
– example port numbers:
– HTTP server: 80
– Mail server: 25
5
App-layer protocol defines
• types of messages exchanged,
– e.g., request, response
• message syntax:
– what fields in messages & how fields are
delineated
• message semantics
– meaning of information in fields
• rules for when and how processes send & respond to
messages

6
Web and HTTP
Review
• web page consists of objects
• object can be HTML file, JPEG image, Java applet,
audio file,…
• web page consists of base HTML-file which includes
several referenced objects
• each object is addressable by a URL, e.g.,

www.someschool.edu/someDept/pic.gif

host name path name

7
HTTP overview

HTTP: hypertext transfer


protocol
• Web’s application layer PC running
protocol Firefox browser
• client/server model
– client: browser that
requests, receives, (using
server
HTTP protocol) and running
“displays” Web objects Apache Web
– server: Web server sends server
(using HTTP protocol)
objects in response to iphone running
requests Safari browser

8
HTTP overview
uses TCP:
• client initiates TCP connection (creates socket) to
server, port 80
• server accepts TCP connection from client
• HTTP messages (application-layer protocol messages)
exchanged between browser (HTTP client) and Web
server (HTTP server)
• TCP connection closed

HTTP is “stateless”
• server maintains no information about past client
requests
9
HTTP connections
non-persistent HTTP
• at most one object sent over TCP connection
– connection then closed
• downloading multiple objects required multiple
connections

persistent HTTP
• multiple objects can be sent over single TCP
connection between client, server

10
Non-persistent HTTP
suppose user enters URL: (contains text,
www.someSchool.edu/someDepartment/home.index references to 10
jpeg images)
1a. HTTP client initiates TCP
connection to HTTP server 1b. HTTP server at host
(process) at www.someSchool.edu waiting for
www.someSchool.edu on port TCP connection at port 80.
80 “accepts” connection, notifying
client
2. HTTP client sends HTTP request
message (containing URL) into TCP
connection socket. Message 3. HTTP server receives request
indicates that client wants object message, forms response message
someDepartment/home.index containing requested object, and
sends message into its socket

time
Non-persistent HTTP (cont.)
4. HTTP server closes TCP connection.

5. HTTP client receives response


message containing html file,
displays html. Parsing html file,
finds 10 referenced jpeg objects

time
6. Steps 1-5 repeated for each of 10
jpeg objects

Application Layer 2-12


Non-persistent HTTP: response time
RTT (definition): time for a small
packet to travel from client to
server and back
HTTP response time: initiate TCP
connection
• one RTT to initiate TCP
RTT
connection
request
• one RTT for HTTP request and file
time to
first few bytes of HTTP RTT transmit
response to return file
file
• file transmission time received
• non-persistent HTTP response
time = time time

2RTT+ file transmission time

13
Persistent HTTP
non-persistent HTTP issues:
• requires 2 RTTs per object
• OS overhead for each TCP connection
• browsers often open parallel TCP connections to fetch
referenced objects

persistent HTTP:
• server leaves connection open after sending response
• subsequent HTTP messages between same client/server
sent over open connection
• client sends requests as soon as it encounters a
referenced object
• as little as one RTT for all the referenced objects
14
two types of HTTP messages:
request, response

15
HTTP request message

• two types of HTTP messages: request, response


• HTTP request message:
– ASCII (human-readable format) carriage return character
line-feed character
request line
(GET, POST, GET /index.html HTTP/1.1\r\n
HEAD commands) Host: www-net.cs.umass.edu\r\n
User-Agent: Firefox/3.6.10\r\n
header Accept: text/html,application/xhtml+xml\r\n
Accept-Language: en-us,en;q=0.5\r\n
lines Accept-Encoding: gzip,deflate\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7\r\n
carriage return, Keep-Alive: 115\r\n
line feed at start Connection: keep-alive\r\n
of line indicates \r\n
end of header lines
16
HTTP request message: general format

method sp URL sp version cr lf request


line
header field name value cr lf

header
~~ ~~ lines

header field name value cr lf


cr lf

~~ entity body ~~ body

17
Uploading form input
POST method:
• web page often includes form input
• input is uploaded to server in entity body

URL method:
• uses GET method
• input is uploaded in URL field of request line

18
Method types
HTTP/1.0: HTTP/1.1:
• GET • GET, POST, HEAD
• POST • PUT
• HEAD • uploads file in entity
– asks server to leave body to path specified
requested object out of in URL field
response • DELETE
• deletes file specified
in the URL field

19
HTTP response message
status line
(protocol
status code HTTP/1.1 200 OK\r\n
status phrase) Date: Sun, 26 Sep 2010 20:09:20 GMT\r\n
Server: Apache/2.0.52 (CentOS)\r\n
Last-Modified: Tue, 30 Oct 2007 17:00:02 GMT\r\n
Accept-Ranges: bytes\r\n
header Content-Length: 2652\r\n
lines Keep-Alive: timeout=10, max=100\r\n
Connection: Keep-Alive\r\n
Content-Type: text/html; charset=ISO-8859-1\r\n
\r\n
data data data data data ...

data, e.g.,
requested
HTML file

20
Request line vs. status line

21
Header format

22
HTTP response status codes
 status code appears in 1st line in server-to-client
response message.
 some sample codes:
200 OK
– request succeeded, requested object later in this msg
301 Moved Permanently
– requested object moved, new location specified later in this msg
(Location:)
400 Bad Request
– request msg not understood by server
404 Not Found
– requested document not found on this server
505 HTTP Version Not Supported
23
Status codes

24
Trying out HTTP (client side) for yourself

1. Telnet to your favorite Web server:

telnet cis.poly.edu 80 opens TCP connection to port 80


(default HTTP server port) at cis.poly.edu.
anything typed in sent
to port 80 at cis.poly.edu

2. type in a GET HTTP request:


GET /~ross/ HTTP/1.1 by typing this in (hit carriage
Host: cis.poly.edu return twice), you send
this minimal (but complete)
GET request to HTTP server

3. look at response message sent by HTTP server!


(or use Wireshark to look at captured HTTP request/response)
25
HttpEcho (Python)
from socket import *
serverPort = 80

#Create a TCP server socket


#(AF_INET is used for IPv4), (SOCK_STREAM is used for TCP)
serverSocket = socket(AF_INET, SOCK_STREAM)

# Bind the socket to server address and server port


serverSocket.bind(("", serverPort))

# Listen to at most 1 connection at a time


serverSocket.listen(1)

26
HttpEcho (Python)
while True:
print('Ready to serve...')
connectionSkt, addr = serverSocket.accept()
message = connectionSkt.recv(1024) # Receives the request message
requested = message.decode()
print('Requested:\n', requested) #print('Received:\n', message)

# Send the HTTP response header line to the connection socket


connectionSkt.send(b'HTTP/1.1 200 OK \r\n')
connectionSkt.send(b'Content-Type: text/plain \r\n')
connectionSkt.send(b'\r\n') # new line

# Send the requested message back to the connection socket


connectionSkt.send(message)
connectionSkt.close() # Close the connection socket

serverSocket.close()

27
HttpEcho (Java version)
import java.io.*;
import java.net.*;
public class HttpEcho {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java HttpEcho [port]");
System.exit(1);
}
try {
int port = Integer.parseInt(args[0]);
ServerSocket ss = new ServerSocket(port);
while (true) {
Socket connect = ss.accept();
handleSocket(connect);
connect.close();
}
} catch(Exception e) { System.out.println(e);}
}
28
HttpEcho (Java version) Contd.
public static void handleSocket(Socket skt) throws IOException{
BufferedReader reader = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
PrintStream out = new PrintStream(skt.getOutputStream());
// Start sending the response, using the HTTP 1.1 protocol
// out.print("HTTP/1.1 404 Not Found \r\n");
out.print("HTTP/1.1 200 OK \r\n"); // Version & status code
out.print("Content-Type: text/html\r\n"); // The type of data
out.print("Connection: close\r\n"); // Will close stream
out.print("\r\n"); // End of headers
String line = null;
while ((line = reader.readLine()) != null) {
if (line.length() == 0) break;
out.print(line + "\r\n");
}
}
}

29
HttpEcho - C# version (Optional)
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;

class HttpEchoProgram {
static void Main(string[] args) {
TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"),
80);
server.Start();
Console.WriteLine("Waiting for Client...");
TcpClient newConn = server.AcceptTcpClient();

IPEndPoint iep = (IPEndPoint) (newConn.Client.RemoteEndPoint);


IPAddress add = iep.Address; int prt = iep.Port;
Console.WriteLine("Connected with a client: {0}: {1} ", add, prt);

NetworkStream stream = newConn.GetStream();


StreamReader sr = new StreamReader(stream);
StreamWriter sw = new StreamWriter(stream);

30
HttpEcho (Contd.)
sw.WriteLine("HTTP/1.1 200 OK");
sw.WriteLine("Content-Type: text/plain");
//sw.WriteLine("Content-Length: size");
sw.WriteLine();

String line = null;


while ((line = sr.ReadLine()).Length != 0) {
Console.WriteLine(line);
sw.WriteLine(line);
sw.Flush();
}
newConn.Close();
server.Stop();
}
}

31
Start your browser at
http://127.0.0.1/index.html

GET /index.html HTTP/1.1


Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

32
SimpleWebServer
• Modify the code of HttpEcho so it can work as a
SimpleWebServer that it fetches requested page in
the local file system and returns it to the browser.
Your web server must be multi-threaded so it handle
each new request with a new thread.

• The browser should display the content of the page


“index.html” if your SimpleWebServer is working
correctly.
• You have to extract the file name of the requested
page from the HTTP request (from the request line);
open it and return its contents to the browser.
33
34

You might also like