Introducing App::htrepl

I've released a (very preliminary) development release of a new project, App::htrepl, a commandline REPL for more easily debugging HTTP applications. It was inspired by a similar project for Node.js, the link to which I can't find at the moment. You can find the repository at github here, and it will be hitting your local CPAN mirror soon.

REPL means read-eval-print loop. App::htrepl provides a commandline script, htrepl, which makes it easier to talk to your HTTP applications. The only dependencies are LWP and Term::ReadLine; the code is otherwise designed to be as lean as possible.

Here are some examples of how it can be used:

Start htrepl from the commandline.

$ htrepl 
htrepl> head http://www.friedo.com/
Setting protocol http
Setting host www.friedo.com
Setting port 80


HEAD http://www.friedo.com:80//

200 OK
Connection: close
Date: Sun, 27 Feb 2011 21:23:14 GMT
Accept-Ranges: bytes
ETag: "ff1f6-88e-87f72640"
Server: Apache/2.2.4 (Fedora)
Content-Length: 2190
Content-Type: text/html; charset=UTF-8
Last-Modified: Tue, 16 Mar 2010 23:41:05 GMT
Client-Date: Mon, 28 Feb 2011 05:54:06 GMT
Client-Peer: 206.71.179.40:80
Client-Response-Num: 1

htrepl automatically determines the Host header and port name, and other details, which it remembers for subsequent requests.

htrepl> get /foobar


GET http://www.friedo.com:80//foobar

404 Not Found
Connection: close
Date: Sun, 27 Feb 2011 21:23:45 GMT
Server: Apache/2.2.4 (Fedora)
Content-Length: 283
Content-Type: text/html; charset=iso-8859-1
Client-Date: Mon, 28 Feb 2011 05:54:36 GMT
Client-Peer: 206.71.179.40:80
Client-Response-Num: 1
Title: 404 Not Found

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /foobar was not found on this server.</p>
<hr>
<address>Apache/2.2.4 (Fedora) Server at www.friedo.com Port 80</address>
</body></html>

If you only want to look at headers, you can throw away the response body:

htrepl> .hide body
Hiding body

htrepl> get /foobar


GET http://www.friedo.com:80//foobar

404 Not Found
Connection: close
Date: Sun, 27 Feb 2011 21:25:51 GMT
Server: Apache/2.2.4 (Fedora)
Content-Length: 283
Content-Type: text/html; charset=iso-8859-1
Client-Date: Mon, 28 Feb 2011 05:56:42 GMT
Client-Peer: 206.71.179.40:80
Client-Response-Num: 1
Title: 404 Not Found

And you can even turn off headers:

htrepl> .hide headers
Hiding headers

htrepl> get /foobar


GET http://www.friedo.com:80//foobar

404 Not Found

htrepl allows you to set arbitrary headers, User-Agent strings (the default is "perl-htrepl/$VERSION") and cookies.

htrepl> .header x-foobar-header blah blah
Setting header x-foobar-header => blah blah

On subsequent requests, the x-foobar-header will be sent, until it's deleted.

htrepl> .cookie mycookie 12345
Setting cookie mycookie => 12345

This saves a cookie in the session, and it will be sent to subsequent requests to the same host. You can inspect your cookies, including those sent from HTTP servers:

htrepl> .show headers
Showing headers

htrepl> get http://www.google.com/
Setting host www.google.com

GET http://www.google.com:80//

200 OK
Cache-Control: private, max-age=0
Connection: close
Date: Mon, 28 Feb 2011 06:02:03 GMT
Server: gws
Content-Type: text/html; charset=ISO-8859-1
Expires: -1
Client-Date: Mon, 28 Feb 2011 06:02:03 GMT
Client-Peer: 72.14.204.104:80
Client-Response-Num: 1
Set-Cookie: PREF=ID=f322e9c1e288ae57:U=2a0bdbd0aa3bc510:FF=0:TM=1298872863:LM=1298872923:S=jMevxo0BYrps9-T2; expires=Wed, 27-Feb-2013 06:02:03 GMT; path=/; domain=.google.com
Title: Google
X-XSS-Protection: 1; mode=block

htrepl> .look cookie PREF
PREF: ID=f322e9c1e288ae57:U=2a0bdbd0aa3bc510:FF=0:TM=1298872863:LM=1298872923:S=jMevxo0BYrps9-T2

If the method name is POST or PUT, htrepl will prompt you for the message data:

htrepl> put /
Enter PUT body data. Terminate with CTRL-d

PUT> blah
PUT> blah blah
PUT> 

PUT http://www.friedo.com:80//

405 Method Not Allowed
Connection: close
Date: Sun, 27 Feb 2011 23:08:08 GMT
Server: Apache/2.2.4 (Fedora)
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Length: 310
Content-Type: text/html; charset=iso-8859-1
Client-Date: Mon, 28 Feb 2011 07:38:59 GMT
Client-Peer: 206.71.179.40:80
Client-Response-Num: 1
Title: 405 Method Not Allowed

You can even load message data from a file:

htrepl> put /foobar < /path/to/some/file.dat

There's also a .help command which will explain most of the commands available. Anything not beginning with a dot is assumed to be an HTTP method name and URI to execute it upon.

TODO:

A lot. There are some bugs with URL-handling that need to be fixed. Support for saving cookies and prefs to disk would be nice. Expanding the docs (there is basically none right now) and cleaning up some of the code would be nice. All patches/pull requests are welcome.

Leave a comment

About Mike Friedman

user-pic Mike Friedman is a professional computer programmer living and working in the New York City area.