H T T P: Yper Ext Ransfer Rotocol
H T T P: Yper Ext Ransfer Rotocol
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
7
HTTP overview
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.
time
6. Steps 1-5 repeated for each of 10
jpeg objects
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
header
~~ ~~ lines
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
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)
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();
30
HttpEcho (Contd.)
sw.WriteLine("HTTP/1.1 200 OK");
sw.WriteLine("Content-Type: text/plain");
//sw.WriteLine("Content-Length: size");
sw.WriteLine();
31
Start your browser at
http://127.0.0.1/index.html
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.