lecture 29 29. lecture 29 lecture goals development of a little web server development of a little...

27
Lecture 29 W W indows indows PROGRAMMING L e c tu r e # L e c tu r e # 29

Upload: evan-rodgers

Post on 26-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

L e c t u r e #L e c t u r e #

29

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

Lecture GoalsLecture Goals Development of a little Web ServerDevelopment of a little Web Server Web Server will server HTTP requests Web Server will server HTTP requests

sent via a Web Browser using URLssent via a Web Browser using URLs URLs:URLs:

http://www.vu.edu.pk/default.htmlhttp://www.vu.edu.pk/index.asphttp://www.vu.edu.pk/win32.htmlhttp://www.vu.edu.pk/courses/win32.html

Lecture 29

WW i n d o w s i n d o w s PROGRAMM INGUniform Resource Uniform Resource

LocatorLocator Anatomy of a URL (Uniform Resource Anatomy of a URL (Uniform Resource

Locator): Locator): http://www.vu.edu.pk/courses/win32.html http:// Protocolhttp:// Protocolwww.vu.edu.pk vu.edu.pk server on vu.edu.pk server on World Wide WebWorld Wide Web/courses/win32.html/courses/win32.html win32.html file win32.html file in courses directory on that server in courses directory on that server

Fetches an HTML file named Fetches an HTML file named win32.htmlwin32.html from from vu.edu.pkvu.edu.pk HTTP Server on the HTTP Server on the World Wide WebWorld Wide Web

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

HTMLHTML HTMLHTML – Hyper Text Mark-up – Hyper Text Mark-up

LanguageLanguage Contains text-formatting information Contains text-formatting information

e.g. font faces, font colours, font e.g. font faces, font colours, font sizes, alignment etc.sizes, alignment etc.

Contains HyperLinks: text that can Contains HyperLinks: text that can be clicked to go to anothre HTML be clicked to go to anothre HTML document on the Internet.document on the Internet.

HTML HTML tagstags are embedded within are embedded within normal text to make it hypertextnormal text to make it hypertext

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

Web BrowserWeb Browser HTTP ClientHTTP Client – a Web Browser e.g. – a Web Browser e.g.

Microsoft Internet Explorer, Microsoft Internet Explorer, Netscape NavigatorNetscape Navigator

Connect to your HTTP web server, Connect to your HTTP web server, requests a document, and displays in requests a document, and displays in its windowits window

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

HTTP ProtocolHTTP Protocol HTTP HTTP – Originally developed by – Originally developed by

PhysicistsPhysicists Meant to share technical HyperText Meant to share technical HyperText

documents across locationsdocuments across locations Clickable HyperText is much easier Clickable HyperText is much easier

to use instead of conventional to use instead of conventional sidebars and indicessidebars and indices

Text-based protocol: meant to Text-based protocol: meant to transport printable text-data and transport printable text-data and NOT binary dataNOT binary data

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

HTTP ProtocolHTTP Protocol

HTTP is a HTTP is a Stateless protocolStateless protocol

• No information or “state” is No information or “state” is maintained about previous HTTP maintained about previous HTTP requestsrequests

• Easier to implement than state-Easier to implement than state-aware protocolsaware protocols

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

Encoding and DecodingEncoding and Decoding

HTTP is a Text Transport ProtocolHTTP is a Text Transport Protocol Transferring binary data over HTTP Transferring binary data over HTTP

needs Data Encoding and Decoding needs Data Encoding and Decoding because binary characters are not because binary characters are not permittedpermitted

Similarly some characters are not Similarly some characters are not permitted in a URL, e.g. SPACE. permitted in a URL, e.g. SPACE. Here, URL encoding is usedHere, URL encoding is used

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

Encoding Example: Escape Encoding Example: Escape SequencesSequences

Including a Carriage Return / Line feed Including a Carriage Return / Line feed in a stringin a string

printf(“Line Oneprintf(“Line One\n\nThis is new line”);This is new line”);

Including a character in a string not Including a character in a string not found on our normal keyboardsfound on our normal keyboards

printf(“The funny character printf(“The funny character \xB2\xB2”);”);

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

URLURL Anatomy of a URL (Uniform Resource Anatomy of a URL (Uniform Resource

Locator): Locator): http://www.vu.edu.pk/courses/win32.html http:// protocolhttp:// protocolwww.vu.edu.pk Web Server Web Servercourses/win32.htmlcourses/win32.html location of file on location of file on serverserver

Or Or http://www.vu.edu.pk:80/.../ .... .... :80 specifies Port Number to use for :80 specifies Port Number to use for connectionconnection

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

Virtual DirectoryVirtual Directory / rerpresents the Home Directory of a / rerpresents the Home Directory of a

Web ServerWeb Server IIS (Internet Information Server) has IIS (Internet Information Server) has

c:\inetpub\wwwroot\ as its default c:\inetpub\wwwroot\ as its default Home DirectoryHome Directory

Here, /courses/ either corresponds to a Here, /courses/ either corresponds to a Physical Directory Physical Directory c:\inetpub\c:\inetpub\wwwroot\courseswwwroot\coursesORORa a Virtual Directoy Virtual Directoy (contd.)(contd.)

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

Virtual Directory (contd.)Virtual Directory (contd.) In a Web Server, we may specify In a Web Server, we may specify

that /courses/ will represent some that /courses/ will represent some other physical directory on the Web other physical directory on the Web Server like D:\MyWeb\. Then /courses/ Server like D:\MyWeb\. Then /courses/ will be a will be a Virtual DirectoryVirtual Directory..

In Windows2000 and IIS 5.0 (Internet In Windows2000 and IIS 5.0 (Internet Information Server), a folder’s “Web Information Server), a folder’s “Web Sharing…” is used to create a Virtual Sharing…” is used to create a Virtual Directory for any folder.Directory for any folder.

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

A Web Browser fetches a pageA Web Browser fetches a page……

http://www.vu.edu.pk/courses/http://www.vu.edu.pk/courses/win32.htmlwin32.html

Hostname/DNS lookup for Hostname/DNS lookup for www.vu.edu.pk to get IP address to get IP address

HTTP protocol uses port 80. Connect HTTP protocol uses port 80. Connect to port 80 of the IP address to port 80 of the IP address discovered above!discovered above!

Request the server for Request the server for /courses/win32.html/courses/win32.html

How?How?

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

HTTP Client RequestHTTP Client Request

• GET GET /courses/win32.html/courses/win32.html HTTP/1.0 HTTP/1.0

• Request line is followed by 2 Request line is followed by 2 Carriage-Return /Line-feed Carriage-Return /Line-feed sequencessequences

Method Resource Identifier

HTTP version

crlf

crlf

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

HTTP Server ResponseHTTP Server Response

• HTTP/1.1 HTTP/1.1 200200 OK OK

• Content-type: Content-type: text/htmltext/html

• Content-length: Content-length: 20612061

• Actual data follows the headersActual data follows the headers

HTTP version

Status Code

Description

crlf

} Status Line

Headers delimited by CR/LF sequence

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

File extensionsFile extensions

File extensions are non-standard across File extensions are non-standard across different platforms and can not be used to different platforms and can not be used to determine the type of contents of any file.determine the type of contents of any file.

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

MIMEMIME

In an HTTP response, a Web Server In an HTTP response, a Web Server tells the browser MIME type of data tells the browser MIME type of data being sentbeing sent

MIME type is used by the browser to MIME type is used by the browser to handle the data appropriately i.e. handle the data appropriately i.e. show an image, display HTML etc.show an image, display HTML etc.

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

File extensions and MIMEFile extensions and MIME

Different common MIME typesDifferent common MIME typesimage/gifimage/gif GIF imageGIF imageimage/jpegimage/jpeg JPEG imageJPEG imagetext/htmltext/html HTML documentHTML documenttext/plaintext/plain plain textplain text

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

HTTP Request HeadersHTTP Request Headers

HTTP request may also contain quite HTTP request may also contain quite a few headers sent by the browsera few headers sent by the browser

HTTP Request Headers usually HTTP Request Headers usually contain information about the contain information about the browser type, client’s IP address, browser type, client’s IP address, screen resolution etc.screen resolution etc.

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

MIMEMIME MIME: Multi-purpose Internet MIME: Multi-purpose Internet

Mail ExtensionsMail Extensions MIME EncodingMIME Encoding features were features were

added to enable transfer of binary added to enable transfer of binary data, e.g. images (GIF, JPEG etc.) via data, e.g. images (GIF, JPEG etc.) via mail.mail.

Using MIME encoding HTTP can Using MIME encoding HTTP can now transfer complex binary data, now transfer complex binary data, e.g. images and videoe.g. images and video

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

RFCRFC Short for Short for RRequest equest ffor or CComments,omments, a series of a series of

notes about the notes about the Internet, started in 1969 (when , started in 1969 (when the Internet was the the Internet was the ARPANET). An Internet ). An Internet Document can be submitted to the Document can be submitted to the IETF by by anyone, but the IETF decides if the document anyone, but the IETF decides if the document becomes an RFC. Eventually, if it gains enough becomes an RFC. Eventually, if it gains enough interest, it may evolve into an Internet interest, it may evolve into an Internet standard..

HTTP version 1.1 is derived from HTTP version 1.1 is derived from HTTP/1.1HTTP/1.1, , Internet RFC 2616, Fielding, et al. Internet RFC 2616, Fielding, et al.

Each RFC is designated by an RFC number. Each RFC is designated by an RFC number. Once published, an RFC never changes. Once published, an RFC never changes. Modifications to an original RFC are assigned a Modifications to an original RFC are assigned a new RFC number. new RFC number.

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

MIME encodingMIME encoding

MIME: Short for MIME: Short for MMultipurpose ultipurpose IInternet nternet MMail ail EExtensions,xtensions, a specification for formatting non-ASCII a specification for formatting non-ASCII messages so that they can be sent over the Internet.messages so that they can be sent over the Internet.

Enables us to send and receive graphics, audio, and Enables us to send and receive graphics, audio, and video files via the Internet mail system.video files via the Internet mail system.

There are many predefined MIME types, such as There are many predefined MIME types, such as GIF graphics files and PostScript files. It is also GIF graphics files and PostScript files. It is also possible to define your own MIME types. possible to define your own MIME types.

In addition to e-mail applications, Web browsers In addition to e-mail applications, Web browsers also support various MIME types. This enables the also support various MIME types. This enables the browser to display or output files that are not in browser to display or output files that are not in HTML format. HTML format.

MIME was defined in 1992 by the Internet MIME was defined in 1992 by the Internet Engineering Task Force (IETF). A new version, Engineering Task Force (IETF). A new version, called S/MIME, supports encrypted messages. called S/MIME, supports encrypted messages.

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

HTTP Status codesHTTP Status codes

404 Not Found404 Not Found   - requested document not found on    - requested document not found on this serverthis server

200 OK200 OK   - request secceeded, requested object    - request secceeded, requested object later in this messagelater in this message

400 Bad Request400 Bad Request   - request message not understood by    - request message not understood by serverserver

302 Object Moved 302 Object Moved    - requested document has been    - requested document has been moved to some other locationmoved to some other location

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

HTTP RedirectionHTTP Redirection

• HTTP/1.1 HTTP/1.1 302302 Object Moved Object Moved

• Location: Location: http://www.vu.edu.pkhttp://www.vu.edu.pk

• Most browsers will send another Most browsers will send another HTTP request to the new location, HTTP request to the new location, i.e. http://www.vu.edu.pki.e. http://www.vu.edu.pk

• This is called Browser RedirectionThis is called Browser Redirection

crlf

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING1 HTTP Request per 1 TCP/IP 1 HTTP Request per 1 TCP/IP

connectionconnection HTML text is received in one HTTP HTML text is received in one HTTP

request from the Web Serverrequest from the Web Server Browser reads all the HTML web Browser reads all the HTML web

page and paints its client area page and paints its client area according to the HTML tags according to the HTML tags specified.specified.

Browser generates one fresh HTTP Browser generates one fresh HTTP request for each image specified in request for each image specified in the HTML filethe HTML file

Lecture 29

WW i n d o w s i n d o w s PROGRAMM ING

Server ArchitectureServer Architecture

Ability to serve up to 5 clients Ability to serve up to 5 clients simultaneouslysimultaneously

Multi-threaded HTTP Web ServerMulti-threaded HTTP Web Server 1 thread dedicated to accept client 1 thread dedicated to accept client

connectionsconnections 1 thread per client to serve HTTP requests1 thread per client to serve HTTP requests 1 thread dedicated to perform termination 1 thread dedicated to perform termination

housekeeping of communication threadshousekeeping of communication threads Use of Synchronisation ObjectsUse of Synchronisation Objects

Lecture 29

WW i n d o w s i n d o w s PROGRAMM INGServer Architecture: Whey Server Architecture: Whey

threads?threads?

Many WinSock function calls e.g. Many WinSock function calls e.g. accept() are blocking callsaccept() are blocking calls

Need to serve up to 5 clients Need to serve up to 5 clients simultaneously using other WinSock simultaneously using other WinSock blocking callsblocking calls

Need to perform termination tasks Need to perform termination tasks for asynchronously terminating for asynchronously terminating communication threadscommunication threads