web development - searchstaff.um.edu.mt/cabe2/lectures/ics2205/tutorials/web_development_2.pdf ·...

33
Web Development Owen Sacco ICS2205/ICS2230 – Web Intelligence

Upload: duongthien

Post on 03-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Web Development Owen Sacco

ICS2205/ICS2230 – Web Intelligence

2. Web Servers

Introduction �  Web content lives on Web servers

�  Web servers speak the platform independent HyperText Transfer Protocol (HTTP) (so they are often called HTTP servers)

�  HTTP: A protocol for transferring requests and files over the Internet or local intranet

�  These HTTP servers store the Internet’s data and provide data when it is requested by HTTP clients

�  Clients send HTTP requests to servers and servers return the requested data in HTTP responses

3

Introduction �  Web clients and servers

4

Introduction �  Multitier Application Architecture (Three-tier

architecture):

5

What is a Web Server? �  The term can refer to either the hardware (i.e. the

computer) or the software (i.e. the application) that delivers web content

�  A Web server software is a server-side application in charge of hosting Web sites

�  The Web server’s primary role is to handle or delegate HTTP requests and to generate or route HTTP responses

6

What is a Web Server? �  The most simplistic form of a Web server may

simply receive GET or POST requests, read a local file based on a requested URL, and stream the file back to the Web client

�  Higher-end enterprise-class Web servers support �  Concurrent requests from scalable number of clients �  Implement some form of secure access control �  Support various APIs for extending the functionality

of a Web server to dynamically generate Web documents in an application-specific fashion

7

What is a Web Server? �  Web servers are available in many forms

�  You can install and run general-purpose software web servers on standard computer systems

�  If you don’t want the hassle of installing software, you can purchase a web server appliance, in which the software comes preinstalled and preconfigured on a computer, often in a snazzy-looking chassis

�  Given the miracles of microprocessors, some companies even offer embedded web servers implemented in a small number of computer chips

8

Tasks performed by a Web Server

1.  Set up connection – accept a client connection, or close the connection if the client is unwanted

2.  Receive a request – read an HTTP request message from the network

3.  Process the request – interpret the request message and take action

4.  Access a resource – access the resource specified in the message

9

Tasks performed by a Web Server

5.  Construct the response – create the HTTP response message with the right headers

6.  Send response – send the response back to the client

7.  Log transaction – place notes about the completed transaction in a log file

10

Tasks performed by a Web Server

11

1. Accepting Client Connections

�  If a client already has a persistent connection open to the server, it can use that connection to send its request

�  Otherwise the client needs to open a new connection to the server

�  When a new client requests a connection (through TCP) to the Web server, the Web server establishes the connection and determines which client is requesting the connection by extracting the IP address from the TCP connection

12

1. Accepting Client Connections

�  Once a new connection is established and accepted the server adds the new connection to its list of existing Web server connections and prepares to watch for data on the connection

�  The Web server is free to reject and immediately close any connection

�  Some Web servers close connections because the client IP address or hostname is unauthorised or is a known malicious client

13

2. Receiving Request Messages

�  As the data arrives on the connection, the Web server reads out the data from the internet connection and parses out the pieces of the request message

�  When parsing the request message, the Web server: �  Parses the request line looking for the request

method, the specified resource identifier (URI) and the version number

�  Reads the message headers �  Detects the end-of-headers blank line �  Reads the request body

14

2. Receiving Request Messages

�  High-performance Web servers support thousands of simultaneous connections

�  Web servers constantly watch for new web requests, because requests can arrive at any time

�  Different Web server architectures service requests in different ways

15

2. Receiving Request Messages

�  Different Web server architectures: �  Single-threaded Web Servers:

�  Process one request at a time until completion

�  When the transaction is complete, the next connection is processed

�  Simple to implement, but during processing, all other connections are ignored

�  Appropriate only for low-load servers

16

2. Receiving Request Messages

�  Different Web server architectures (contd.): �  Multi-process and Multithreaded Web Servers:

�  Dedicate multiple processes or higher efficiency threads to process requests simultaneously

�  Threads/processes may be created on demand or in advance

�  Some servers dedicate a thread/process for every connection – this may consume too much memory or system resources when a server processes hundreds, thousands or even tens of thousands of simultaneous connections

17

2. Receiving Request Messages

�  Different Web server architectures (contd.): �  Multiplexed I/O Web Servers:

�  To support large numbers of connections, many Web servers adopt multiplexed architectures

�  In a multiplexed architecture, all the connections are simultaneously watched for activity

�  Work is done on a connection only when there is something to be done; threads and processes are not tied up waiting on idle connections

18

3. Processing Requests �  Once the Web server has received a request it can

process the request using the method, resource, headers, and optional body

�  Some methods, example POST require entity body data in the request message

�  Other methods, example GET, forbid entity body data in request messages

19

4. Mapping and Accessing Resources

�  Web servers are resource servers

�  They deliver pre-created content, such as HTML pages or JPEG images, as well as dynamic content from resource-generating applications running on the servers

�  Before the Web server can deliver content to the client, it needs to identify the source of the content, by mapping the URI from the request message to the proper content or content generator on the Web server

20

4. Mapping and Accessing Resources

�  Web servers support different kinds of resource mapping

�  The simplest form of resource mapping uses the request URI to name a file in the Web server’s file system

�  Typically, a special folder in the Web server file system is reserved for Web content

�  This folder is called the document root

21

4. Mapping and Accessing Resources

�  The Web server takes the URI from the request message and appends it to the document root

�  Virtually hosted Web servers host multiple Web sites on the same Web server, giving each site its own distinct document root on the server

�  A virtually hosted Web server identifies the correct document root to use from the IP address or hostname in the URI or the Host header

�  This way, two Web sites hosted on the same Web server can have completely distinct content

22

4. Mapping and Accessing Resources

�  Web servers can also map URIs to dynamic resources – that is to programs that create content on demand

�  These Web servers are called Application Servers that connect Web servers to sophisticated backend applications

23

5. Building Responses �  Once the Web server has identified the resource, it

performs the action described in the request method and returns the response message

�  The response message contains: �  HTTP version �  Response status code �  Response headers �  A response body if one was generated

�  Web servers sometimes return redirection responses instead of success messages that redirect the browser to go elsewhere

24

6. Sending Responses �  The Web server may have many connections to

many clients: �  Some idle

�  Some sending data to the server, and �  Some carrying response data back to the clients

�  The server needs to keep track of the connection state and handle persistent connections with special care

25

6. Sending Responses �  For non-persistent connections, the server is

expected to close its side of the connection when the entire message is sent

�  For persistent connections, the connection may stay open, in which case the server needs to be extra cautious to compute the Content-Length header correctly, or the client will have no way of knowing when a response ends

26

7. Logging �  When a transaction is complete, the Web server

notes an entry into a log file, describing the transaction performed

�  Most Web servers provide several configurable forms of logging

27

Accessing Web Servers �  To request documents from Web servers, users

must know the hostname on which the Web server software resides

�  Users can request documents from local Web servers �  Web servers residing on User’s machine �  Can be accessed through your computer’s name or

through the name localhost – a hostname that references the local machine and normally translates to the IP address 127.0.0.1 (known as the loopback address)

28

Accessing Web Servers �  Users can request documents from remote Web

servers �  Web servers residing on different machines

�  Referenced by a fully qualified hostname or an IP address

29

Web Servers

Server September 2013 Percent October 2013 Percent

Apache 346,288,706 46.86% 344,408,387 44.89%

Microsoft IIS 160,691,763 21.74% 177,216,296 23.10%

nginx 111,680,078 15.11% 123,114,800 16.05%

Google GWS 34,806,502 4.71% 34,127,482 4.45%

30

�  The market share of the top Web servers on the internet1

1 http://news.netcraft.com/archives/2013/10/02/october-2013-web-server-survey.html

Installing Apache HTTP Server

�  The Apache HTTP Server, maintained by the Apache Software Foundation, is the most popular Web server in use today because of its stability, efficiency, portability, security and small size

�  It is open source and it runs on Linux, Mac OS X, Windows and numerous other platforms

�  The Apache HTTP server can be downloaded and installed directly, however this requires additional configuration on your part

31

Installing Apache HTTP Server

�  There are many integrated installers that install and configure Apache HTTP Server for you on various operating-system platforms

�  For simplicity, we’ll use the free and open source XAMP integrated installer provided by the Apache Friends website (www.apachefriends.org)

�  The advantage of this integrated installer is that apart from installing the Apache HTTP server, it will install and configure PHP and MySQL as well for you!!

32

Installing Apache HTTP Server

�  Other integrated installers: �  WAMP – Windows Apache MySQL PHP �  LAMP – Linux Apache MySQL PHP �  MAMP – Mac OS X Apache PHP

�  Testing Your setup: once you have started the servers, you can open any Web browser on your computer and enter the address http://localhost/ to confirm that the Web server is up and running

�  If it is, you’ll see the default XAMP webpage displayed

33