Skip to content

Commit 9d2edab

Browse files
committed
adds chapter 6.2 user agents
1 parent ca5a2e1 commit 9d2edab

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,7 @@ The repo for our backend framework- [Velocy](https://github.com/ishtms/velocy)
185185
- [Caching improvements](/chapters/ch06.1-http-verbs.md#caching-improvements)
186186
- [Range Requests](/chapters/ch06.1-http-verbs.md#range-requests)
187187
- [New Methods: `PUT`, `DELETE`, `CONNECT`, `OPTIONS`, `TRACE`](/chapters/ch06.1-http-verbs.md#new-methods-put-delete-connect-options-trace)
188+
- [User Agents](/chapters/ch06.2-user-agents.md)
189+
- [`User-Agent` can be weird](/chapters/ch06.2-user-agents.md#user-agent-can-be-weird)
188190

189191
![](https://uddrapi.com/api/img?page=readme)

assets/imgs/user-agent.png

284 KB
Loading

chapters/ch01-what-is-a-web-server-anyway.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# What the hell is a web server any way?
44

5-
> Note: This chapter gives you a brief overview of the basics of web servers and HTTP. We'll learn about HTTP in more details in an [upcoming chapter](/chapters/ch06-http-deep-dive.md).
5+
> Note: This chapter gives you a brief overview of the basics of web servers and HTTP. We'll learn about HTTP in more details in an [upcoming chapter](/chapters/ch06.0-http-deep-dive.md.md).
66
77
If you do not wish to read about the basics of web/http, you can safely jump to the [coding section](/chapters/ch02-your-first-nodejs-server.md).
88

chapters/ch06.0-http-deep-dive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,6 @@ This is the actual response body returned by the server. In this case, it's a si
257257
258258
Now that we understand what are the basic components of the `request` and the `response`, let's understand these components in more detail, in the next chapter
259259
260-
[![Read Prev](/assets/imgs/next.png)](/chapters/ch06.1-http-verbs.md)
260+
[![Read Prev](/assets/imgs/next.png)](/chapters/ch06.1-http-verbs-versioning-http1_1.md)
261261
262262
![](https://uddrapi.com/api/img?page=http_deep_dive)

chapters/ch06.1-http-verbs-versioning-http1_1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,6 @@ We talked about `PUT` and `DELETE` methods in the previous section. We'll focus
373373
374374
We covered the first two lines of the `cURL` output in this chapter. In the next one, we'll take a look at **user agents**.
375375
376+
[![Read Prev](/assets/imgs/next.png)](/chapters/ch06.2-user-agents.md)
377+
376378
![](https://uddrapi.com/api/img?page=http_verbs)

chapters/ch06.2-user-agents.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![Read Prev](/assets/imgs/prev.png)](/chapters/ch06.1-http-verbs-versioning-http1_1.md)
2+
3+
## User agents
4+
5+
A user agent is any software client that sends a request for data or information. One of the most common type of user agent are the web browsers. There are many other types of user agents as well. These include crawlers (web-traversing robots), command-line tools, billboard screens, household appliances, scales, light bulbs, firmware update scripts, mobile apps, and communication devices in various shapes and sizes.
6+
7+
It's worth noting that the term "user agent" doesn't imply a human user is interacting with the software agent at the time of the request. In fact, many user agents are installed or configured to run in the background and save their results for later inspection or to only save a subset of results that might be interesting or erroneous. For instance, web-crawlers are typically given a starting URL and programmed to follow specific behavior while crawling the web as a hypertext graph.
8+
9+
In our `cURL` response, we have the user-agent listed on this line:
10+
11+
```bashag-0-1h8ot3rujag-1-1h8ot3ruj
12+
> User-Agent: curl/7.87.0
13+
```
14+
15+
Here, the user-agent is the `cURL` command line program that we used to make the request to our server. But, it get's more strange when you make a request from a browser. Let's see that in action.
16+
17+
### `User-Agent` can be weird
18+
19+
Open chrome or any browser of your choice, and open the web console using `Command + Option + J` on a mac or `Control + Shift + J` on windows or linux. Type the following command in the console and press enter:
20+
21+
```js
22+
navigator.userAgent;
23+
```
24+
25+
This outputs:
26+
27+
![](/assets/imgs/user-agent.png)
28+
29+
Ha, such a weird looking string isn't it? What are 3 browsers doing there? Shouldn't it be only `Chrome`?
30+
31+
This phenomenon of "pretending to be someone else" is quite prevalent in the browser world, and almost all browsers have adopted this strategy for practical reasons. A great article that you should read if you're interested is the [user-agent-string-history](https://webaim.org/blog/user-agent-string-history/).
32+
33+
For instance, let's examine the example in question: 
34+
35+
```bash
36+
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
37+
```
38+
39+
It is a representation of Chrome 116 on a Macintosh. But why is it showing Mozilla, even though we are not using it?
40+
41+
```bash
42+
Why does a browser behave that way?
43+
```
44+
45+
The answer is simple. Many websites and web pages are designed to identify which browser is visiting the page, and only deliver certain features if the browser is Chrome/Mozilla/... (although this is a BAD programming practice, (we'll get to it in a bit) it happens, especially in the early days of the Internet). In such cases, if a new browser is released and wants to provide the same features for that page, it must pretend to be Chrome/Mozilla/Safari
46+
47+
*Does that mean that I cannot find the information I need from the `User-Agent` header?*
48+
49+
Not at all. You can definitely find the information you need, but you must match the `User-Agent` precisely with the User-Agent database. This can be a daunting task because there are more than [**219 million**](https://explore.whatismybrowser.com/useragents/explore/) user agent strings!
50+
51+
Like I told earlier, checking the type of browser to do things separately is a very bad idea. Some reasons include:
52+
53+
- Malicious clients can easily manipulate or spoof user agents, causing the information provided in the user agent string to be potentially inaccurate.
54+
- Modern browsers often use similar user agent structures to maintain compatibility with legacy websites, making it difficult to accurately identify specific browsers or features based solely on user agents.
55+
- The rise of mobile devices and Internet of Things (IoT) devices has led to a diverse range of user agents, making it challenging to accurately identify and categorize all types of devices.
56+
- Some users might switch user agents to access restricted content or to avoid being tracked, resulting in unexpected behavior when making decisions based on user agent data.
57+
- Many alternative and lesser-known browsers and user agents do not follow standard patterns, so relying solely on well-known user agents could exclude valid users.
58+
- Tying application behavior too closely to specific user agents can hinder future updates or improvements and complicate the development process by requiring constant adjustments.
59+
60+
You can find a very detailed explanation on why this is bad here - [Browser detection using the user agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent) and what alternate steps you could take in order to create a feature that's not supported on some browsers.
61+
62+
![](https://uddrapi.com/api/img?page=user-agents)

0 commit comments

Comments
 (0)