john smith
john smith

Reputation: 649

HTTP: Why do you need to specify hostname?

I'm starting to learn sockets, and I'm trying to figure out why you need to specify the hostname in the request. If I am already connected to "www.google.com", then what is the point of "Host: www.google.com\r\n" in the request? Doesn't the server already know its own name?

Upvotes: 5

Views: 9388

Answers (2)

Thanatos
Thanatos

Reputation: 44246

Sometimes the server does know its hostname, but it isn't always a 1:1 relationship. Many DNS hostnames can all point to the same IP, thus, 1 IP can have many DNS hostnames. (Also, a single DNS hostname can map to many IP addresses, but this is less important in this question.)

The Host: line in an HTTP request allows a web server to know which hostname you requested, and serve based on that. This allows one machine at an IP address to serve many domains.

For example, if a webserver sees Host: foo.com, it might serve one website, but Host: bar.com might result in a completely different result being returned. There is no other piece of data available to the webserver with this information, and it relies on the web client to inform it.

Upvotes: 8

Mat
Mat

Reputation: 206659

A given server can have many different DNS names. Think shared hosting sites for example.

Upvotes: 3

Related Questions