Index
Intro
URLs
URL Components
Internet Protocol (IP) Suite
The DNS Lookup Process
TCP (Transmission Control Protocol)
The Three Way Handshake
HTTP Verbs/Methods
API Endpoints
What is HTTP (HyperText Transfer Protocol)?
Request
Response
HTTP Status Codes
HTTP is Stateless
What is the REST (REpresentational State Transfer) Architecture?
What is the Web Layers Architecture?
Intro
- Internet existed since around 1960s
- Usage was minimal and restricted
- In 1989 (at CERN), Sir Tim Berners-Lee invented HTTP (Hypertext Transfer Protocol); insight was to use the existing hypertext (documents that had links to other documents) could be moved throughout the internet (linking to other documents addresses instead of another document in the same space).
- First HTTP version was 0.9
1.0 URLs
- Address of a resource on the internet (ex: https://example.com)
- Basis of communication with URLs is the request and response pattern.
- A client (maybe a web browser, maybe an app, anything that sends requests to servers) requests information to a server which sends a response
1.0.1 URL Components
For https://www.python.org/about
- A scheme:
https://
- A hostname:
www.python.org
- A(n optional) path:
/about
1.1 Internet Protocol (IP) Suite
- In the process of sending a request and receiving a response, other tech must work together with the client and server to load the webpage
- This is broadly referred to as the internet protocol suite.
1.1.1 The DNS Lookup Process
- User types a TLD (Top-Level Domain) http://www.google.com into their web browser
- Browser sends request to DNS (Domain Name System) server
- DNS lookup occurs (usually the one provided by the user's ISP (Internet Service Provider), can also be a public DNS); human-readable name is mapped to server's IP as registered in the DNS (can be in the cache, if nothing is found then looks up authoritative nameservers)
- DNS returns IP address to browser
- OS that runs the browser caches the information locally and uses the IP address to connect directly to the website
Note: Most DNS problems (such as unexistent or old versions of websites) are usually on the client-side. These are usually solved by clearing the local DNS cache.
1.1.2 TCP (Transmission Control Protocol)
- Once a client has an IP address for a domain, it sets up a consistent connection with a server
- This happens via TCP (Transmission Control Protocol) which provides reliable, ordered and error-checked delivery of bytes between two computers
- To Establish a TCP connection between two computers, a three-way "handshake" occurs between client and server:
1.1.2.1 The Three Way Handshake
- Client sands a SYN asking to establish a connection
- Server responds with a SYN-ACK acknowledging the request and passing a connection parameter
- Client sends an ACK back to the server to confirm the connection
- Once the TCP connection is established, the two computers can start communicating via HTTP
1.1.3 HTTP Verbs/Methods
- Every webpage contains an address (URL) and a list of approved actions (HTTP verbs)
- Here is a more thorough list, below are the most common ones.
CRUD | HTTP Verb/Methods |
---|---|
Create | POST |
Read | GET |
Update | PUT |
Delete | DELETE |
1.1.4 API Endpoints
- A traditional website consists of pages with HTML, CSS, images, javascript, etc.
- A web API also relies on URLs but instead of serving webpages it produces API endpoints
- An endpoint contains data (usually in JSON) and a list of available actions (HTTP Verbs)
1.1.5 What is HTTP (HyperText Transfer Protocol)?
- HTTP is a request-response protocol between two computers that have an existing TCP connection.
- Client makes requests, server returns responses
1.1.5.1 Request
Example request:
GET / HTTP/1.1
Host: google.com
Accept_Language: en-US
- Line 1: request line specifies HTTP method to use, path & HTTP version
- Two subsequent one are HTTP headers: Host is the domain name and Accept_Language is the language to use.
- See here a more thorough list of HTTP headers Note: HTTP messages might also contain a body, only used with HTTP responses containing data.
1.1.5.2 Response
This assumes that Google's homepage only contains the HTML "Hello, world!".
HTTP/1.1 200 OK
Date: Mon, 24 Jan 2022 23:26:07 GMT
Server: gws
Accept-Ranges: bytes
Content-Length: 13
Content-Type: text/html; charset=UTF-8
Hello, world!
- Top line is the response line, specifies HTTP version and response status code
- Next 5 lines are HTTP header.
- After the line break there is the body content of "Hello, world!"
Note: most web pages contain multiple resources that require multiple HTTP request/response cycles. A webpage with HTML, a linked CSS file and one image has three separate trips back-and-forth between client and server before the complete page can be rendered.
1.1.5.3 HTTP Status Codes
- Once a client sends a request, this does not mean the request will simply proceed
- There is a vast list of HTTP Status codes. The examples below are the most common ones.
HTTP Status Code | Description |
---|---|
2xx Success | The action requested by the client was received, understood, and accepted |
3xx Redirection | The requested URL has moved (usually will be followed by further action, e.g. sending a new request to a new URL returned by the server) |
4xx Client Error | There was an error, typically a bad URL request by the client |
5xx Server Error | The server failed to resolve a request |
2.0 HTTP is Stateless
- HTTP is stateless; this means each request/response pair is independent of others. There is no stored memory of past interactions.
- This also applies to 3xx responses, as the final result is basically "instructions of what to do next"
- This is an engineering decision due to signal loss over time, a very long communication means that everything would potentially fall apart with one error; HTTP as is means that if a single cycle does not go through, it can simply be repeated without problems for the rest of the communication chain
Note: while HTTP itself is stateless, there can be transfer of stateful data among a client and a server, such as user authentication (a browser stores an auth token as a cookie and sends to a specific IP on each request to this IP and the server checks the token validity to authenticate and authorize the user before each response).
3.0 What is the REST (REpresentational State Transfer) Architecture?
REpresentational State Transfer is an architecture proposed in 2000 by Roy Fielding in a dissertation thesis. It is an approach to build APIs on top of the HyperText Transfer Protocol.
This defines how the API will communicate externally; it does not care about internal details or separation of concerns.
Every RESTful API:
- is stateless (every request/response cycle is independent; see Websockets for an example of stateful communication)
- supports common HTTP verbs on endpoints (GET, POST, PUT, DELETE, etc)
- returns data in either JSON or XML formats
4.0 What is the Web Layers Architecture?
This broadly defines the internal composition of the web app (note that APIs will lack the Presentation Layer).
Each layer has specific responsibilities and will communicate with adjacent layers; it is used to promote separation of concerns and modular design.
Presentation Layer (PL):
- Handles user interface and user interaction
- Typically includes HTML, CSS, and JavaScript
- Responsible for displaying data and capturing user input
Example: a rendered webpage with HTML, CSS and Javascript.
Business Logic Layer (BLL):
- Contains the core functionality and business rules of the application
- Processes data and implements application logic
- Sits between the presentation and data layers
Example: a controller function that checks authorization for an user and schedules a particular job on the server to be completed at a later date.
Data Service Layer (DSL):
- Acts as an intermediary between the business logic and data access layers
- Provides an additional level of abstraction for data operations
- Can improve security and modularity of more complex applications
Example: callbacks that perform validations at a model-level before proceeding with data-altering procedures
Data Access Layer (DAL):
- Manages data storage and retrieval
- Interacts with databases or other data sources
- Abstracts data operations from the business logic layer
Example: implementation of CRUD operation functions that will alter or retrieve data from a database.
Top comments (0)