DNS & HTTP
programming abstraction: socket
(today: client-side socket)
(later this week: server-side socket & case study - node.js)
what happens when browsing http://www.cs.uw.edu/
protocol: HTTP
domain name: www.cs.uw.edu
(IP address: 128.208.3.118
)
port: 80 (default)
q: can we directly use 128.208.3.118
in browser?
[see also: OSI model]
application layer: DNS, HTTP, SSH, …
transport layer: TCP, UDP, …
internet layer: IPv4, IPv6, …
translate www.cs.uw.edu
to 128.208.3.118
(DNS)
connect to 128.208.3.118
(TCP)
send request to fetch data (HTTP)
a naming system (recall file system)
map domain names to IP addresses
example: www.cs.uw.edu
→ 128.208.3.118
q: can one domain name map to multiple IP addresses?
q: can multiple domain names map to the same IP address?
a single file (HOSTS.TXT
or /etc/hosts
)? scalability?
hierarchy
root: .
generic top-level domain (gTLD): edu
[draw google.com, mit.edu]
run: dig . ns
demo: resolve www.cs.uw.edu
q: where’s mit.edu
(from Seattle)?
manpage: getaddrinfo
, freeaddrinfo
, getnameinfo
MIT 6.033’s DNS hands-on
take CSE 461
now we have the IP address
how to connect to the server and fetch data?
// return a socket (file descriptor)
int socket(int domain, int type, int protocol);
// connect to a server
int connect(int fd, const struct sockaddr *addr, socklen_t addrlen);
use socket
/connect
to start a client-side socket (not open
)
you can use read
/write
on sockets (or recv
/send
)
close
when done
request/response protocol, over TCP, default port 80
client (e.g., browser) sends a request
server produces a response
HTTP/1.1 textual (HTTP/2 binary)
[method] [uri] HTTP/[version]\r\n
[name]: [value]\r\n
...
[name]: [value]\r\n
\r\n
[body]
methods: GET, POST, HEAD, …
demo (chrome & httpie): http -v www.cs.uw.edu
HTTP/[version] [status code] [reasons]\r\n
[name]: [value]\r\n
...
[name]: [value]\r\n
\r\n
[body]
status code
1xx: some kind of informational message
2xx: success of some kind
3xx: redirects the client to a different URL
4xx: the client’s request contained some error
5xx: the server experienced an error
server-side socket