Вы находитесь на странице: 1из 23

HTTP Client & Server Interaction

Bob Dickerson
Dept. of Computer Science, University of Hertfordshire
HTTP Client & Server Interaction p.1/25
What happens when a browser contacts a server

this example shows the statements executed in a browser


and a server, and also what they do,

this example assumes that the server is running on a host


with address 3.3.3.3,

the server program is using port 8888,

the le request is tiny.html

therefore the request URL is:


http://3.3.3.3:8888/tiny.html
but this is, of course, not sent in this form. . .
HTTP Client & Server Interaction p.2/25
Initial request
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
SERVER
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Request is entered into browser
(but server hasnt started)
server-client01a
HTTP Client & Server Interaction p.3/25
Browser prepares request
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
SERVER
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Browser prepares to send request to HTTP server,
(but server still hasnt started)
server-client01b
HTTP Client & Server Interaction p.4/25
Connection fails
connect port 8888
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
SERVER
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
ERROR no such port
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Client cannot make a connection,
server isnt listening.
server-client02a
HTTP Client & Server Interaction p.5/25
Browser tells user of failure
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
SERVER
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Browser announces failure to connect
server-client02b
HTTP Client & Server Interaction p.6/25
Server binds to 8888
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Server tells its operating system that
it will accept connections to port 8888
server-client03
HTTP Client & Server Interaction p.7/25
Server executes accept and waits
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Server executes accept and awaits a connection from a client
server-client04
HTTP Client & Server Interaction p.8/25
Browser does request again
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Browser requests page again
server-client05a
HTTP Client & Server Interaction p.9/25
Browsers operating systems connects
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server connect port 8888
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Browser (the client) asks its operating system to connect to
port 8888 on server 3.3.3.3
server-client05b
HTTP Client & Server Interaction p.10/25
Servers operating system accepts
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
OK connected
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
The server programs operating system replies OK
because port 8888 exists and a program is listening
server-client06
HTTP Client & Server Interaction p.11/25
Connected socket is created
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
Both operating systems create a connected socket and
return descriptors to their programs (the browser and the server)
server-client07
HTTP Client & Server Interaction p.12/25
Browser builds request
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
GET /tiny.html HTT
P/1.1\r\nHost: 3...
f=open(name..);
write(s,hdr...);
hdr
tiny.html
This..
<h1>Ex.
parag..
The browser constructs a request string:
GET /tiny.html HTTP/1.1\r\n....
server-client08
HTTP Client & Server Interaction p.13/25
Browser sends request, server reads it
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
write(s,hdr...);
f=open(name..);
hdr
tiny.html
GET /tiny.html HTT
GET /tiny.html HTT
P/1.1\r\nHost: 3...
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
This..
<h1>Ex.
parag..
The browser writes the request to the socket and
it is sent to the HTTP server where it is read into req
server-client09
HTTP Client & Server Interaction p.14/25
Example of a full browser request
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
write(s,hdr...);
f=open(name..);
hdr
tiny.html
GET /tiny.html HTT
GET /tiny.html HTT
P/1.1\r\nHost: 3...
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
This..
<h1>Ex.
parag..
The only really essential bits are: the rst request line, and the blank line at the
end. Most of the other lines are optional and vary between browsers.
server-client09b
HTTP Client & Server Interaction p.15/25
Server analyses and checks request
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
f=open(name..);
write(s,hdr...);
hdr
tiny.html
GET /tiny.html HTT
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
This..
<h1>Ex.
parag..
The server analyses the request: it checks if the le
exists and the request is legal, it is ok so. . .
server-client10
HTTP Client & Server Interaction p.16/25
Server opens le
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
f=open(name..);
write(s,hdr...);
Open files:
f>tiny.html
hdr
tiny.html
GET /tiny.html HTT
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
This..
<h1>Ex.
parag..
The server opens the le
server-client11
HTTP Client & Server Interaction p.17/25
Server sends OK header
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
read(s,resp..);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
f=open(name..);
write(s,hdr...);
Open files:
f>tiny.html
hdr
tiny.html
GET /tiny.html HTT
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
HTTP/1.1 200 OK\r\n
Server: Apache..
HTTP/1.1 200 OK\r\n
Server: Apache..
HTTP/1.1 200 OK\r\n
Server: Apache..
This..
<h1>Ex.
parag..
BUT before it can send the le it must send the correct
HTTP response lines: HTTP/1.1 200 OK\r\nServer:...
server-client12
HTTP Client & Server Interaction p.18/25
Server reads le
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
f=open(name..);
write(s,hdr...);
Open files:
f>tiny.html
read(s,resp..);
read(s,resp+..);
buf
tiny.html
GET /tiny.html HTT
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
HTTP/1.1 200 OK\r\n
Server: Apache..
<h1> Example Page <
/h1>\r\nThis is the
This..
<h1>Ex.
parag..
The server now reads the requested le from disc
into a buffer (character array) called buf
server-client13
HTTP Client & Server Interaction p.19/25
Server writes the le contents to the socket
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
f=open(name..);
write(s,hdr...);
Open files:
f>tiny.html
read(s,resp..);
read(s,resp+..);
buf
tiny.html
GET /tiny.html HTT
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
HTTP/1.1 200 OK\r\n
Server: Apache..
<h1> Example Page <
/h1>\r\nThis is the
<h1> Example Page <
/h1>\r\nThis is the
<h1> Example Page <
/h1>\r\nThis is the
This..
<h1>Ex.
parag..
The server writes the contents of buf
to the socket and it is read by the browser
server-client14
HTTP Client & Server Interaction p.20/25
Example of possible full response
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
f=open(name..);
write(s,hdr...);
Open files:
f>tiny.html
read(s,resp..);
read(s,resp+..);
buf
tiny.html
GET /tiny.html HTT
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
HTTP/1.1 200 OK\r\n
Server: Apache..
<h1> Example Page <
/h1>\r\nThis is the
<h1> Example Page <
/h1>\r\nThis is the
<h1> Example Page <
/h1>\r\nThis is the
This..
<h1>Ex.
parag..
Possible response including required header line, some optional lines, a blank line
and the le.
server-client14b
HTTP Client & Server Interaction p.21/25
How the full response is sent

No server should ever just send a le (HTML page, or


image), it must send a response code and header lines rst,

the header lines and le are separated by a blank line,

clearly the server code might have separate write


statements to the socket for the response lines (constructed
as strings in memory) and the le (which is read from disc),

however because this will be faster than the network speed


the client might see it all as one stream of bytes,

whether the browser reads it all in one read or repeated


reads in a loop is a separate issue,

it should use the Content-Length: line OR detect the


socket closing, to decide when to stop.
server-client14c
HTTP Client & Server Interaction p.22/25
Browser checks response and shows le
Connected
sockets:
server s>
browser s
s=Socket(a,8888);
req="GET.....";
write(s,req...);
req
resp
p=8888; a="3.3.3.3";
BROWSER
req
ss=ServerSocket(p);
s=Accept(ss);
read(s,req...);
// analyse req
read(f,buf..);
write(s,buf..);
while(! eof){
Listening
ports:
8888>
SERVER
server
Connected
sockets:
browser s>
server s
f=open(name..);
write(s,hdr...);
Open files:
f>tiny.html
read(s,resp..);
read(s,resp+..);
buf
tiny.html
GET /tiny.html HTT
P/1.1\r\nHost: 3...
GET /tiny.html HTT
P/1.1\r\nHost: 3...
HTTP/1.1 200 OK\r\n
Server: Apache..
<h1> Example Page <
/h1>\r\nThis is the
<h1> Example Page <
/h1>\r\nThis is the
This..
<h1>Ex.
parag..
The browser checks the response header and because its OK
it displays the contents of the le following the header
server-client15
HTTP Client & Server Interaction p.23/25

Вам также может понравиться