Kiến trúc Web

Nội dung

 Kiến trúc web

 Giao thức HTTP

 Mô hình MVC

Kiến trúc Web

 Kiến trúc Web Platform

 Platform

• Hardware • Software modules & configurations • Choice of software platform (e.g., J2EE, .NET)

 Kiến trúc Web Application

 Mô hình hóa các tiến trình xử lý  Trong một lĩnh vực cụ thể  Module hóa và phức tạp

Kiến trúc Web Platform

 Web platform dựa trên các thành phần sau

 TCP/IP  HTTP  HTML

 Mô hình kiến trúc Client/Server  Là mô hình phức tạp…

 Các thành của network (firewall, proxy, load

balancer)

 Các thành phần của intranet (web server,

application server, database, web services)

Mô hình Web (tầng ứng dụng)

 Các thành phần ngữ nghĩa

 Dịch vụ Web: Nội dung

động và có tính tương tác  PHP, Python, Java

của Web  URLs/URIs  HTTP  HTML

HTML/XHTML for display

HTTP for transport

URL/URI for addressing

PHP, python etc. for interaction

HTTP Server

Clients (browsers)

Mô hình Web (các tầng giao thức)

Client

Web server

HTTP message

HTTP

HTTP

TCP segment

TCP

TCP

Router

Router

IP packet

IP packet

IP packet

IP

IP

IP

IP

Ethernet interface

Ethernet interface

SONET interface

SONET interface

Ethernet interface

Ethernet interface

Ethernet

SONET link

Ethernet

Giao thức HTTP

 Những bước xử lý của browser

http://origin/..

DNS query

DNS server

Client

Origin server

Establish TCP connection

HTTP request

HTTP transaction

HTTP response

Optional parallel connections

HTML

Giao dịch của HTTP  Một giao dịch của giao thức HTTP là một request và một response (một phản hồi của một request nào đó) giữa web client và web server

Client

Origin server

GET / HTTP/1.1 Accept: image/gif, image/x-xbitmap, image/ jpeg, image/pjpeg, */* Accept-Language: en-us Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT) Host: www.eurecom.fr Connection: Keep-Alive

HTTP/1.1 200 OK Date: Mon, 15 Jul 2002 08:49:00 GMT Server: Apache/1.3.26 (Unix) PHP/4.2.1 Last-Modified: Wed, 12 Jun 2002 08:49:49 GMT ETag: "2a-50ea-3d070b2d" Accept-Ranges: bytes Content-Length: 20714 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html ...

HTML

HTTP Message

 HTTP message ở dạng ASCII text, gồm:

 Một dòng yêu cầu

Method Request-URI HTTP-Version (Request-Line)

HTTP-Version Status-Code Reason-Phrase (Status-Line)

 Các trường Header

field-name ":" [ field-value ]

 Một dòng trống

 Phần thân thông báo (tùy chọn)

HTTP Request

 Ví dụ GET /foo.html HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/ jpeg, image/pjpeg, */* Accept-Language: en-us Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT) Host: www.eurecom.fr Connection: Keep-Alive

GET /foo.html HTTP/1.1 — request "/foo.html" using HTTP version 1.1 Accept: — types of documents accepted by browser Accept-Language: — preferred language is english Accept-Encoding: — browser understands compressed documents User-Agent: — identification of browser (real type is IE 5.01) Host: — what the client thinks the server host is Connection: — keep TCP connection open until explicitly disconnected

HTTP Response

 Ví dụ: HTTP/1.1 200 OK

Date: Mon, 15 Jul 2002 08:49:00 GMT Server: Apache/1.3.26 (Unix) PHP/4.2.1 Last-Modified: Wed, 12 Jun 2002 08:49:49 GMT ETag: "2a-50ea-3d070b2d" Accept-Ranges: bytes Content-Length: 20714 Connection: close Content-Type: text/html ...

HTTP/1.1 200 OK — document found (code 200); server is using HTTP 1.1 Date: — current date at the server Server: — software run by the server Last-Modified: — most recent modification of the document ETag: — entity tag (unique identifier for the server resource, usable for caching) Accept-Ranges: — server can return subsections of a document Content-Length: — length of the body (which follows the header) in bytes Connection: — the connection will close after the server's response Content-Type: — what kind of document is included in the response ... — document text (follows blank line)

Client Methods

 GET

 DELETE

Retrieve a resource from the Remove a resource from the

server

 OPTIONS (HTTP 1.1)

 HEAD

server (static file, or dynamically-generated data)

Request other options for an

Get information about a

 POST

 TRACE (HTTP 1.1)

resource (but not the actual resource) URI (methods understood by a server or allowed for a given URI)

Client provides some Ask proxies to declare

 PUT

Used for HTTPS (secure HTTP) through a proxy

information to the server, e.g., through forms (may update the state of the server) themselves in the headers (used for debugging)  CONNECT (HTTP 1.1)

Provide a new or replacement resource to put on the server

HTTP Caching

 Dữ liệu Cache gần người dùng hơn

 Improves Web performance, reduces load on server  Cache control directives in HTTP header (no cache, age, etc.)

GET http://origin/..

Cache server

Origin server

Client 1

GET http://origin/..

200 OK

200 OK

Client 2

GET http://origin/..

200 OK

HTTP Client Authentication

 Cơ chế bảo mật đơn giản thông qua username/password  Basic scheme: username:password base-64 encoded echo -n “user:password" | openssl base64 echo "c2NvdHQ6dGlnZXI=" | openssl base64 -d

GET /private/ HTTP/1.1

Client

Origin server

HTTP/1.1 401 Unauthorized WWW-Authenticate: Basic realm="secret"

Username: joe Password: ********

GET /private/ HTTP/1.1 Authorized: Basic SHY3GH7D3SH==

HTTP/1.1 200 OK ...

HTML

Cookies  Cho phép Web servers lưu

Client

Origin server

GET http://origin/..

trạng thái của Client  Dùng cặp khóa – giá trị  Thường dùng quản lý các phiên giao dịch session  Cookie có thể truy xuất bởi

200 OK Set-Cookie: ABC=XYZ

một site/domain

 Server có thể định thời gian

hết hạn

GET http://origin/.. Cookie: ABC=XYZ

200 OK

 Client có thể từ chối cookie, giới hạn kích thước, thời gian…

Mô hình: Model View Controller

 Mô hình được sử dụng trong Smalltalk

(1979)

 Tách riêng giữa tầng dữ liệu và hiển thị  Dễ dàng phát triển

Mô hình: Model View Controller

 Model

 Tượng trưng cho dữ liệu trong phần mềm

ứng dụng

 View

 Tầm nhìn hay khung nhìn (view) bao gồm các

thành phần của giao diện người dùng

 Controller

 Quản lý sự trao đổi dữ liệu  Các nguyên tắc nghề nghiệp trong các thao

tác liên quan đến mô hình

Model-View-Controller 2 cho Web  Sự thích ứng của MVC đối với Web

 Kết nối không bền vững giữa client và server  Thông báo những thay đổi  Truy vấn lại server để phát hiện những thay

đổi

Client/Server (2-Layer)

Client

Client

Server

Web/App Server Services

Database Dynamic HTML

Static HTML

Client/Server (2-Layer)

N-Layer Architectures

Client

Firewall

Proxy

Presentation Layer Web Server

Business Layer

Application Server (Business Logic, Connectors, Personalization, Data Access) Backend (Legacy Application, Enterprise Info System)

Data Layer

DBMS B2B

N-Layer Architectures

Vì sao dùng N-Layer Architecture?

 Tách các dịch vụ trong quá trình xử lý

nhằm tái sử dụng chúng  Kết nối lỏng lẻo nhằm giảm ảnh hưởng lên cả

hệ thống

 Dễ bảo trì (in terms of code)  Có thể mở rộng (modular)

 Trade-offs

 Giảm sự phức tạp  Tăng khả năng chịu lỗi

JSP-Model-1 Architecture

JSP-Model-2 Architecture

1. user request 2. create/change model 3. create/change view 4. generate output 5. server response

Struts Architecture

1. user request 2. forwarding to controller 3. create/change model 4. create/change view 5. generate output 6. server response