Monitoring HTTP Traffic to Debug Your Grails Application

| Comments

I’m often surprised when I run across developers who don’t have a variety of HTTP traffic monitoring apps as an integral part of their toolkit. When doing Grails development (or any web development really), being able to see the actual information that is going over the wire is invaluable.

Firebug

These days, more and more people are familiar with the amazing Firefox plugin Firebug. It allows you to view request and response information along with many other tricks.

firebug showing grails.org response

There have been times that Firebug seems to swallow requests and not display them. I’ve also seen situations where it seems like it’s making a second request when you open up the response tab in firebug’s window. Firebug is also only available when you’re working with Firefox.

Wireshark/Ethereal

In times like these, it’s nice to have alternatives to turn to. Wireshark (née Ethereal), is often cited as the cross-platform swiss army knife of packet sniffers. One problem with swiss army knives is that it can often be difficult to determine which part of the tool is the right one for the job. It’s open source and free, and there is a lot of power if you’re willing to dig in to the documentation a little.

HTTPScoop

If you’re on a Mac, I’ve found that HTTPScoop works really well as a focused HTTP monitoring utility with a nice GUI. It’s shareware and costs a reasonable 10 GBP (which, with the falling dollar is getting less reasonable, but still worth it).

If you’re debugging requests to your local machine, all you need to do is set it to monitor the “lo0” interface. This is the loopback interface that is used for localhost requests.

httpscoop - setting lo0 interface for localhost

Then, in your preferences, you’ll want to only monitor port 8080 (or whatever port you have Grails running on).

httpscoop - prefs set to monitor port 8080

After that, just hit the “Scoop” button and it will monitor all traffic going to localhost:8080. You can see a number of details about each including headers, request/response body, HTTP status codes, etc.

httpscoop - sample request/response

tcpdump

If you’re looking for quick, dirty, and free, tcpdump should be at the top of your list. If you’re on OS X or Linux, you’ve probably already got it on your machine and don’t need to worry about installing anything.

Like wireshark above, it’s a powerful swiss army knife and it can be difficult to wade through the man page to figure out how to get it to do exactly what you want. That’s why I’ve set up this alias for myself so I don’t have to remember the voodoo incantation to call it:

alias tcpd8080="sudo tcpdump -s 0 -A -i lo0 'tcp port 8080 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'"

If you put that in your .profile/.bash_profile/.zshrc, all you need to do is type tcpd8080 and it will automatically start monitoring the loopback interface for traffic to localhost:8080. The extra bits on the end tell tcpdump to only monitor tcp requests on port 8080 and the final portion is used to filter out unecessary IPv4 SYN/FIN/ACK requests that are part of the communication between client and server.

Here’s a sample request/response captured by tcpdump to a sample grails application:

  22:56:54.395121 IP localhost.62581 > localhost.http-alt: P 417:835(418) ack 106 win 65535 
E.....@.@............u....Al...H...........
./.../..GET /library/ HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300

Connection: keep-alive22:56:55.209576 IP localhost.http-alt > localhost.62581: P 106:1569(1463) ack 835 win 65535 
E.....@.@..............u...H..C............
./.../..HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 1360
Server: Jetty(6.1.4)

    
        Welcome to Grails
        
        
        
        

    
        
        
        

Welcome to Grails

Congratulations, you have successfully started your first Grails application! At the moment this is the default page, feel free to modify it to either redirect to a controller or display whatever content you may choose. Below is a list of controllers that are currently deployed in this application, click on each to execute its default action:

There are a number of other tools out there that do similar things, including proxies like Fiddler and TCPMon, but I find those more cumbersome to use for most situations.

Comments