shawn a
shawn a

Reputation: 809

UNIX C HTTP request returning 301 Moved Permanently

I am familiar with the 301 error code but new to http requests and formatting them correctly.

In my program i need to retrieve my school's homepage, but i get a 301 Moved Permanently header. The header's location says where the page moved to, but even that new location won't work for me, probably because i didn't format it correctly.

Initially i send this request:

GET / HTTP/1.1\r\nHost: www.cs.uregina.ca\r\nConnection: close\r\n\r\n

And receive this header:

Received: HTTP/1.1 301 Moved Permanently
Date: Tue, 04 Nov 2014 05:38:42 GMT
Server: Apache
Location: http://www.cs.uregina.ca/
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

What should my new HTTP request look like to get the above moved webpage?

If i try the location of the moved page like it suggests then i get the following 400 Bad Request Response:

GET / HTTP/1.1\r\nHost: http://www.cs.uregina.ca\r\nConnection: close\r\n\r\n

Received: HTTP/1.1 400 Bad Request
Date: Tue, 04 Nov 2014 05:52:36 GMT
Server: Apache
Content-Length: 334
Connection: close
Content-Type: text/html; charset=iso-8859-1

Upvotes: 1

Views: 1848

Answers (2)

Nisse Engström
Nisse Engström

Reputation: 4752

Initially i send this request:

GET / HTTP/1.1\r\nHost: www.cs.uregina.ca\r\nConnection: close\r\n\r\n

And receive this header:

Received: HTTP/1.1 301 Moved Permanently
...
Location: http://www.cs.uregina.ca/
...

This is exactly what I get when I request cs.uregina.ca. You have probably connected to cs.uregina.ca (or some subdomain other than www), or to an IP address the does not correspond to www.cs.uregina.ca.

If i try the location of the moved page like it suggests then i get the following 400 Bad Request Response:

GET / HTTP/1.1\r\nHost: http://www.cs.uregina.ca\r\nConnection: close\r\n\r\n

Received: HTTP/1.1 400 Bad Request
...

This is not surprising. You must remove the http:// protocol from the Host: header. Eg:

GET / HTTP/1.1\r\nHost: www.cs.uregina.ca\r\nConnection: close\r\n\r\n

In general, when requesting a URL such as the following:

     http://domain.example:80/path/to/resource/?query#fragment
     ----   -------------- ==------------------------
   protocol      host      |        path
                          port

you would:

  • resolve the host name to an IP address, and connect to that IP address on port (if present in the URL) or the default port associated with the protocol.
  • Communicate with the server using a mechanism specific to protocol. In this case, an HTTP request.
  • Request path from the server with an appropriate Host: header (in case there are multiple hosts on the same IP).
  • The fragment identifier is used with (X)HTML and is not actually sent to the server.

The request should (at a minimum) look like this:

GET /path/to/resource/?query HTTP/1.1
Host: domain.example
Connection: close

The full details can be found in:

Upvotes: 1

Nick Wilkerson
Nick Wilkerson

Reputation: 385

If you just want the homepage, download nc and type "nc www.cs.uregina.ca 80" When nc starts type the following and then hit return twice:

GET http://www.cs.uregina.ca HTTP/1.0

Upvotes: 0

Related Questions