Search this site

Metadata

Articles

Projects

Presentations

View http headers, or, how lwp-request sucks

I normally use HEAD(1) to view HTTP response headers. However, when developing web applications outside of Apache or any other standard web server, it's often likely that you won't immediately support HEAD requests. This is a problem if all you want to do is see the http headers.

Perl's libwww comes with a tool called lwp-request, which is installed with symlinks GET, HEAD, etc. You can use GET and have it show only the HTTP headers. Or can you?

The response message I am looking for is this:

HTTP/1.0 200 OK
Server: PasteWSGIServer/0.5 Python/2.4.3
Date: Tue, 19 Dec 2006 22:42:20 GMT
content-type: text/html; charset=UTF-8
Connection: close
The response I get using GET -sed is:
200 OK
Connection: close
Date: Tue, 19 Dec 2006 22:43:16 GMT
Server: PasteWSGIServer/0.5 Python/2.4.3
Content-Type: text/html; charset=UTF-8
Client-Date: Tue, 19 Dec 2006 22:43:16 GMT
Client-Peer: 127.0.0.1:5000
Client-Response-Num: 1
Link: </css/style.css>; media="all"; rel="stylesheet"; type="text/css"
Link: </css/tabs.css>; media="all"; rel="stylesheet"; type="text/css"
Link: </css/tabs-ie.css>; media="all"; rel="stylesheet"; type="text/css"
Title: pimp: music for you.
What? LWP appears to be doing some html parsing. :(

So, a solution usiung netcat, which is trash, because lwp should just not be stupid by default. All I want are http headers. Here's my netcat solution:

% echo "GET / HTTP/1.0\r\n\r\n" | nc localhost 5000  | col | sed -e '/^$/q'
HTTP/1.0 200 OK
Server: PasteWSGIServer/0.5 Python/2.4.3
Date: Tue, 19 Dec 2006 23:00:12 GMT
content-type: text/html; charset=UTF-8
Connection: close
Oh look! The headers I wanted. I hate you, LWP.

Sigh.


3 responses to 'View http headers, or, how lwp-request sucks'

Showing last 3 comments... (Click here to view all comments)

Dan wrote at Sun Dec 31 18:52:47 2006...
This works too, and lets curl worry about how to request the document:

curl -o /dev/null -D - -s http://www.semicomplete.com/

Of course, you do need curl installed, but it isn't that uncommon, and sometimes useful. I tried to get it working with wget, but wget always wants to display the progressbar, etc.

Dan

Techniq wrote at Wed Feb 21 22:08:47 2007...
Better yet try either of these:

# curl --head www.apache.org
HTTP/1.1 200 OK
Date: Thu, 22 Feb 2007 03:07:38 GMT
Server: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.7g
Accept-Ranges: bytes
Cache-Control: max-age=86400
Expires: Fri, 23 Feb 2007 03:07:38 GMT
Vary: Accept-Encoding
Content-Length: 16752
Content-Type: text/html

# echo HEAD / HTTP/1.1 | nc www.apache.org 80
HTTP/1.1 400 Bad Request
Date: Thu, 22 Feb 2007 03:08:27 GMT
Server: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.7g
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=iso-8859-1

Jordan Sissel wrote at Thu Feb 22 01:35:14 2007...
Techniq,

First, I need to send a GET request, not a HEAD request. If all I want is to send a HEAD request, I'll just use HEAD(1) which comes with perl lwp (or any other ways like curl --head). However, I specifically want a GET request and only see the headers.

Second, your nc example doesn't work becuase you're not actually sending a valid HTTP request.

HTTP requests have lines terminated with "\r\n" not just "\n". Furthermore, you don't have an empty line after your request (also required)

This pushes a HEAD request:
% echo "HEAD / HTTP/1.1\r\n\r\n" | nc www.apache.org 80
Date: Thu, 22 Feb 2007 06:30:49 GMT
Server: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.7g
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=iso-8859-1


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: