Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Example of a TCP/IP Application: the World Wide Web

Similar presentations


Presentation on theme: "An Example of a TCP/IP Application: the World Wide Web"— Presentation transcript:

1 An Example of a TCP/IP Application: the World Wide Web
Babak Esfandiari (plus some material by Qusay Mahmoud, Roger Impey, and the textbooks)

2 The World Wide Web No need to introduce the Web, is there?
A uniform resource locator: URL A protocol: HTTP The client: a Web browser The server: the Web server A markup language: HTML Server-side dynamic generation of HTML documents: CGI, Servlets, ASPs, JSPs… Client-side rendering: Stylesheets, JavaScript, Java, Flash…

3 WWW Architecture

4 HTTP Hypertext Transport Protocol Client/server Transaction-oriented
Not limited to hypertext though! Client/server Transaction-oriented Stateless

5 The Uniform Resource Locator (URL)
Not limited to HTTP: protocol://host:port/resource_path The browsers default to: http protocol Port 80 Index.html resource Resources can be static or dynamic

6 The HTTP Protocol RFCs 1945 and 2068 for versions 1.0 and 1.1 respectively HTTP transactions consist in a request and a response Two types of request methods: GET and POST

7 GET Request Simple get request: (HTTP 0.9) Full get request:
GET /document.html [CRLF] Full get request: GET /document.html HTTP/1.0 [CRLF] Full get request with headers: If-Modified-Since: Sun 20 Oct :07:51 GMT[CRLF] [LF]

8 Post request Post allows the client to include a body of data in a request: POST /cgi-bin/code.cgi HTTP/1.0 [CRLF] Content-type: application/octet-stream [CRLF] Content-length: 2048 [CRLF] [LF] body

9 HTTP Responses Simple Response: body Full Response:
HTTP/ OK[CRLF] [LF] body Full Response with headers: Content-type: text/html[CRLF]

10 HTTP Response codes Here a few response codes: OK Bad Request
Unauthorized Not Found Internal Server Error

11 MIME types Originally designed for , associates a type with a message to help the receiver to decode/view (RFC 1521) Can be used in a HTTP header Type/subtype Common types/subtypes: text/html, text/plain, image/gif…

12 A simple HTTP Server See textbook!

13 Programming Web Applications
CGI Servlets JSP What do these have in common? They are all server-side technologies!

14 CGI What is CGI How does it work? Environment variables
Processing Forms GET vs. POST Examples

15 What is CGI? Stands for “Common Gateway Interface”
Server-side technology Can be used: To Process fill-out forms To generate dynamic contents By a web server to run external programs By a web server to get/send data from databases and other apps

16 How does it work? HTTP Receive Request CGI Process Fork Process
Gen. Response Receive Output Send Response

17 CGI CGI scripts can be written in any language, including Java
Perl is the most popular for CGI scripting To experiment, you need a web server: Xitami ( Jakarta-Tomcat (jakarta.apache.org/tomcat) Tomcat supports Servlets/JSP

18 Content-type: text/html\n\n
Content headers If your script generates HTML then use: Content-type: text/html\n\n This tells the browser what content it is about to receive Other content headers (MIME!) include: text/plain image/gif image/jpg

19 Sample Script #!/usr/bin/perl print "Content-type:text/html\n\n";
print "<html><head><title>Test Page</title></head>\n"; print "<body>\n"; print "<h2>Hello, world!</h2>\n"; print "</body></html>\n";

20 Environment Variables
Some of the environment variables: DOCUMENT_ROOT HTTP_HOST HTTP_USER_AGENT REMOTE_HOST REQUEST_METHOD QUERY_STRING CONTENT_LENGTH …etc

21 Script: environment variables
#!/usr/bin/perl print "Content-type:text/html\n\n"; print <<EndOfHTML; <html><head><title>Print Environment</title></head> <body> EndOfHTML foreach $key (sort(keys %ENV)) { print "$key = $ENV{$key}<br>\n"; } print "</body></html>";

22 Forms <form action="env.cgi" method="GET">
Enter some text here: <input type="text" name="sample_text" size=30><input type="submit"><p> </form>

23 Forms As you know now, there are two ways to send data from an HTML form to a CGI script GET POST These methods determine how the form data is sent to the server

24 GET The input values from the form are sent as part of the URL
They are saved in the QUERY_STRING environment variable If in the above example you type: “hello there John” The QUERY_STRING will be: Sample_text=hello+there+John Spaces have been replaced with +

25 GET…. This is called URL Encoding! Some commonly encoded characters
\t (tab) %09 \n (return) %0A / %2F ~ %7E : %3A ; %3B @ %40 & %26

26 GET…. If input is: Sarah Johnson
<form action="env.cgi" method="GET"> First Name: <input type="text" name="fname“ size=30><p> Last Name: <input type="text" name="lname" size=30><p> <input type="submit"> </form> If input is: Sarah Johnson $ENV{‘QUERY_STRING’} would be: fname=Sarah&lname=Johnson

27 GET…. Parsing: @values = split(/&/,$ENV{'QUERY_STRING'});
foreach $i { ($varname, $mydata) = split(/=/,$i); print "$varname = $mydata\n"; }

28 GET…. It is possible to send values as part of a URL
Hidden values can be used to maintain session info

29 POST More sophisticated than GET
Data is not sent as URL-encoded (I.e. not part of the URL) When POST is used, data is sent as a separate message (input stream)

30 POST…. Parsing: read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer); foreach $pair { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; }


Download ppt "An Example of a TCP/IP Application: the World Wide Web"

Similar presentations


Ads by Google