5. HTTP Protocol
• Protocol between Browser <-> Web server
• Initially used to transmit documents in HTML format
• We know GET method now, but how about post
something to webserver?
7. Perform a POST in HTTP
POST /register HTTP/1.1
Host: www.facebook.com
HTTP/1.1 200 OK
HTML
save to
database
surname=E&name=ensky&email=…
8. HTTP usage
• Actually, HTTP is very simple, trivial, and easy to use
in many situations
• When it comes to data-manipulation, there are four
basic method:
Create, Read, Update, Delete(CRUD)
they can be perfectly mapped into HTTP protocol
9. HTTP protocol -> REST API
• Using HTTP protocol to serve an data-manipulation
API, we call it REST API.
• WIKI: REST
Operation HTTP
Read GET
Create POST
Update PUT
Delete DELETE
10. Imagine you're writing a game
• you'll have
– character info
• maybe life, age, attack power, skills, location, …
– Monster info
• maybe life, skills, …
– and many data need to be CRUD
11. REST API
• And it'll be very easy if you use REST API.
– GET /characters/1/life
– PUT /characters/1
life=60&isPoisoned=1
– PUT /characters/1/location
map=1&block=123
19. HTML hello world
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello world! Title</title>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
20. HTML is a nested language
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello world! Title</title>
</head>
<body>
<p>Hello world!</p>
</body>
</html>
21. HTML is a markup language
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>…</title>
</head>
<body>
<p>…</p>
</body>
</html>
• Tag
• Attribute
• Value
• Node
• Parent
• Children
22. HTML is a markup language
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>…</title>
</head>
<body>
<p>…</p>
</body>
</html>
• Head defines
something for
browser
• Encoding
• Title of the
page
• Resources
• CSS
• Javascript
23. HTML is a markup language
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>…</title>
</head>
<body>
<p>…</p>
</body>
</html>
• Body defines
something to
render
(the contents)
24. Common attributes
<p id="unique" class="red-div" title="help text">HIHI</p>
• class
classify elements, usually set for CSS or JS to select some
elements. One element can have multiple classes
• id
same as class, but unique in a document.
One element can only have one id
• style
inline CSS
• title
help text when hover
30. 1.Initialize a socket
wait for client's HTTP request
GET /icon.png HTTP/1.1
Host: www.ensky.tw
HTTP/1.1 200 OK
HTML
port 80
31. 2.Parse the HTTP request
find out some useful information
like URL, Hostname, …
GET /icon.png HTTP/1.1
Host: www.ensky.tw
HTTP/1.1 200 OK
HTML
32. 3.Prepare file according to
above information
GET /icon.png HTTP/1.1
Host: www.ensky.tw
HTTP/1.1 200 OK
HTML
get icon.png from
somewhere in your file system
34. Virtual host
• One web server can host many domains
• It can be determined by "Host: " part in HTTP
protocol
www.facebook.com
GET /enskylin HTTP/1.1
Host: www.facebook.com
35. Virtual host
• In case of static file mapping, you can simply do
www.facebook.com -> c:Domainswww
image.facebook.com -> c:Domainsimage
so any request to http://www.facebook.com/song.mp3
web server will try to find the file at
c:Domainswwwsong.mp3
38. The only difference is we get data
from database
rather than disk file
GET /enskylin HTTP/1.1
Host: www.facebook.com
HTTP/1.1 200 OK
HTML
39. And we need to generate the HTML
GET /enskylin HTTP/1.1
Host: www.facebook.com
HTTP/1.1 200 OK
HTML
40. And send it back to browser
GET /icon.png HTTP/1.1
Host: www.ensky.tw
HTTP/1.1 200 OK
HTML
41. If you want to write a web server
1. Initialize a socket to wait for client's HTTP
request
2. Parse the HTTP request and find out some
useful information like URL, Host, …
3. Prepare document according to above
information
4. Response the document back to client
42. If you want to write a web server
1. Initialize a socket to wait for client's HTTP
request
2. Parse the HTTP request and find out some
useful information like URL, Host, …
3. Prepare document according to above
information
4. Response the document back to client
43. there is different logic for each page
-> it is hard to write in Web server
GET /enskylin HTTP/1.1
Host: www.facebook.com
HTTP/1.1 200 OK
HTML
44. If you want to write a web server
1. Initialize a socket to wait for client's
HTTP request
2. Parse the HTTP request and find out
some useful information like URL,
Host, …
3. Prepare document according to above
information
4. Response the document back to client
web
Server
CGI
45. CGI and Web server
Web server
CGI
HTTP Request
stdin + env
stdout
HTTP
Response
+ BODY
HTTP
request
body
HTTP
request
header
HTTP
response
head + body