Deprecated Behaviour

The inane, sometimes insane, ramblings from the mind of Brenton Alker.

Replay Web Hook Requests With Netcat

Netcat (nc) is a really useful little utility, available for most (all?) OSs. It’s often used for low level network tinkering. Recently, I found an everyday (for me) use; Testing HTTP “Web Hooks”.

In my specific case, it was the Facebook “Real-Time” API, which POSTs data back to your registered endpoint when a given event occurs on Facebook. But, navigating to Facebook, performing an action and waiting for them to notify your server is a relatively slow process, and makes debugging painfully slow.

To overcome this, we need to be able to consistently repeat a request from Facebook while fine-tuning the handler to perform the required task.

Firstly, we can set up nc to capture the request. We could manually write a HTTP request, but this will ensure it is authentic and actually represents the request that will be sent by the third party.

nc -l 8000 > request.txt

This will cause nc to listen on port 8000 and write any incoming HTTP requests to “request.txt”. Then, we just need to coerce the target service to send us a request at the correct location (you could use port 80, if you don’t need to keep the web server running). Note: the listening nc process will not reply to the request, so the connection will stay open until the client times out or you manually kill the process. Once the request is received, it will be stored in “request.txt”, where we can view it, edit it and – most importantly – replay it.

We can also use nc to handle sending the request for us by piping the saved file through to the target server.

cat request.txt | nc myserver.example.com 80

This will connect to our server and make the exact HTTP request that was captured. The advantage, of course, is that we can replay the request over and over quickly and accurately.

Remapping Caps Lock to Ctrl in OS X

As a keyboard line junky and a Vim user, this is something I’ve been meaning to do for a long time. I mean, who actually uses Caps lock anyway? Every time I’ve had a look, the process has appeared to involve installation of third party software and in general seemed like too much effort. It turns out, if you only want to remap meta-keys (caps lock, alt, ctrl and command) you can do that easily through the OSX System Preferences.

In “Apple Menu” (Top Left), “System Preferences”, “Keyboard”. On the “Keyboard” Tab (not “Keyboard Shortcuts”), there is a “Modifier Keys” button, which opens a dialog and provides a simple interface to remap (or disable) your modifier keys.

I wish I’d realised sooner that it was so simple, now to re-train my hands and free that poor little finger from its curled up hell.

Applying Functional Concepts to OOP: Referential Transparency / Command-Query Separation

I’ve been studying functional programming lately (it’s trendy) and while it’s great to just learn new languages and paradigm, it’s always nice when the principles can be applied to everyday work.

Command-Query separation is probably one of the lesser known principles of object-oriented programming (OOP). Proposed by Bertrand Meyer in “Object Oriented Software Construction”, it states that a function (or method) should perform a Command (do something) or a Query (return a value) but not both. So, any function that returns a value should not modify state. In Martin Fowler’s discussion of the principle he mentions that “it would be nice if the language itself would support this notion”.

If occurs to me that functional languages do indeed grant Mr. Fowler his wish.

Functional programmers aim to compose programs using “pure” functions. In this case, “pure” is used to mean a functions that are “referentially transparent”, that is, they only depend upon their parameters. A pure function, given the same set of parameters, will always return the same result. No external influences, including variables, databases, inputs or outputs can be used or modified by its execution.

Of course, a program that takes no input, can’t access a data store and produces no output isn’t very useful. As such, even the most pure of the functional languages have to allow the nasty real-world into its pristine clean-room. The difference though, is the strict boundaries that are constructed to protect the pure world from the impure. In Haskell, which is often held up as the purest of functional languages, this airlock is provided by the IO Monad. I won’t go into what a Monads are, it doesn’t matter, and I’m not sure I even explain them even if I wanted to. The point is, there are language features that strictly separate functions that alter state with those that “only” perform calculation.

These referentially transparent “pure” functions are revered because they can be reused, re-ordered or parallelized and are guaranteed to always produce the same result. This makes programs more predictable and thus simpler to debug. When you know a function is pure, you need only check its return value; it can’t have changed anything outside of itself.

So, while the languages most of us use daily don’t offer this as a feature, maybe following the principle more often would make for more predictable, bug free code. Wouldn’t that be nice.