server client01

Upload: milos-stojanovic

Post on 03-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Server Client01

    1/23

    HTTP Client & Server Interaction

    Bob Dickerson

    Dept. of Computer Science, University of Hertfordshire

    HTTP Client & Server Interaction p.1/25

  • 8/12/2019 Server Client01

    2/23

    What happens when a browser contacts a server

    this example shows the statements executed in a browserand a server, and also what they do,

    this example assumes that the server is running on a host

    with address3.3.3.3, the server program is using port8888,

    the file request istiny.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

  • 8/12/2019 Server Client01

    3/23

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

    parag..

    Request is entered into browser(but server hasnt started)

    server-client01a

    HTTP Client & Server Interaction p.3/25

  • 8/12/2019 Server Client01

    4/23

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

    parag..

    Browser prepares to send request to HTTP server,(but server still hasnt started)

    server-client01b

    HTTP Client & Server Interaction p.4/25

  • 8/12/2019 Server Client01

    5/23

    Connection fails

    connect port 8888s=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..Ex.

    parag..

    Client cannot make a connection,server isnt listening.

    server-client02a

    HTTP Client & Server Interaction p.5/25

  • 8/12/2019 Server Client01

    6/23

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

    Ex.

    parag..

    Browser announces failure to connect

    server-client02b

    HTTP Client & Server Interaction p.6/25

  • 8/12/2019 Server Client01

    7/23

    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){

    Listeningports:8888>

    SERVER

    server

    f=open(name..);write(s,hdr...);

    hdr

    tiny.html

    This..Ex.

    parag..

    Server tells its operating system thatit will accept connections to port 8888

    server-client03

    HTTP Client & Server Interaction p.7/25

  • 8/12/2019 Server Client01

    8/23

    Server executesacceptand 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..Ex.

    parag..

    Server executes accept and awaits a connection from a client

    server-client04

    HTTP Client & Server Interaction p.8/25

  • 8/12/2019 Server Client01

    9/23

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

    parag..

    Browser requests page again

    server-client05a

    HTTP Client & Server Interaction p.9/25

  • 8/12/2019 Server Client01

    10/23

    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

    serverconnect port 8888

    f=open(name..);write(s,hdr...);

    hdr

    tiny.html

    This..Ex.

    parag..

    Browser (the client) asks its operating system to connect toport 8888 on server 3.3.3.3

    server-client05b

    HTTP Client & Server Interaction p.10/25

  • 8/12/2019 Server Client01

    11/23

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

  • 8/12/2019 Server Client01

    12/23

    Connected socket is created

    Connectedsockets: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

    Connectedsockets:browser s>

    server s

    f=open(name..);write(s,hdr...);

    hdr

    tiny.html

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

  • 8/12/2019 Server Client01

    13/23

    Browser builds request

    Connectedsockets: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

    Connectedsockets:browser s>

    server s

    GET /tiny.html HTT

    P/1.1\r\nHost: 3...

    f=open(name..);write(s,hdr...);

    hdr

    tiny.html

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

  • 8/12/2019 Server Client01

    14/23

    Browser sends request, server reads it

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...

    This..Ex.

    parag..

    The browserwrites the request to the socket andit is sent to the HTTP server where it isreadintoreq

    server-client09

    HTTP Client & Server Interaction p.14/25

  • 8/12/2019 Server Client01

    15/23

    Example of a full browser request

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...

    This..Ex.

    parag..

    The only really essential bits are: the first request line, and the blank line at theend. Most of the other lines are optional and vary between browsers.

    server-client09b

    HTTP Client & Server Interaction p.15/25

  • 8/12/2019 Server Client01

    16/23

    Server analyses and checks request

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...

    This..Ex.

    parag..

    The server analyses the request: it checks if the fileexists and the request is legal, it is ok so. . .

    server-client10

    HTTP Client & Server Interaction p.16/25

  • 8/12/2019 Server Client01

    17/23

    Server opens file

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...

    This..Ex.

    parag..

    The server opens the file

    server-client11

    HTTP Client & Server Interaction p.17/25

  • 8/12/2019 Server Client01

    18/23

    Server sends OK header

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...

    HTTP/1.1 200 OK\r\n

    Server: Apache..

    HTTP/1.1 200 OK\r\nServer: Apache..

    HTTP/1.1 200 OK\r\nServer: Apache..

    This..Ex.

    parag..

    BUT before it can send the file it mustsend the correctHTTP response lines:HTTP/1.1 200 OK\r\nServer:...

    server-client12

    HTTP Client & Server Interaction p.18/25

  • 8/12/2019 Server Client01

    19/23

    Server reads file

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...

    HTTP/1.1 200 OK\r\nServer: Apache..

    Example Page \r\nThis is the

    This..Ex.

    parag..

    The server now reads the requested file from discinto a buffer (character array) calledbuf

    server-client13

    HTTP Client & Server Interaction p.19/25

  • 8/12/2019 Server Client01

    20/23

    Server writes the file contents to the socket

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...HTTP/1.1 200 OK\r\nServer: Apache..

    Example Page \r\nThis is the

    Example Page \r\nThis is the

    Example Page \r\nThis is the

    This..Ex.

    parag..

    The server writes the contents ofbufto the socket and it is read by the browser

    server-client14

    HTTP Client & Server Interaction p.20/25

  • 8/12/2019 Server Client01

    21/23

    Example of possible full response

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...HTTP/1.1 200 OK\r\nServer: Apache..

    Example Page \r\nThis is the

    Example Page \r\nThis is the

    Example Page \r\nThis is the

    This..Ex.

    parag..

    Possible response including required header line, some optional lines, a blank lineand the file.

    server-client14b

    HTTP Client & Server Interaction p.21/25

  • 8/12/2019 Server Client01

    22/23

    How the full response is sent

    No server should ever just send a file (HTML page, orimage), it must send a response code and header lines first,

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

    clearly the server code might have separatewritestatements to the socket for the response lines (constructedas strings in memory) and the file (which is read from disc),

    however because this will be faster than the network speedthe client might see it all as one stream of bytes,

    whether the browser reads it all in onereador repeated

    reads in a loop is a separate issue, it should use theContent-Length: line OR detect the

    socket closing, to decide when to stop.

    server-client14c

    HTTP Client & Server Interaction p.22/25

  • 8/12/2019 Server Client01

    23/23

    Browser checks response and shows file

    Connectedsockets: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

    Connectedsockets: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 HTTP/1.1\r\nHost: 3...HTTP/1.1 200 OK\r\nServer: Apache..

    Example Page \r\nThis is the

    Example Page \r\nThis is the

    This..Ex.

    parag..

    The browser checks the response header and because its OKit displays the contents of the file following the header

    server-client15

    HTTP Client & Server Interaction p.23/25