Fun with CFHTTPMessage, Headers and the HTTP Standard

TL:DR; CFHTTPMessage combines duplicate headers to a single string value containing all headers comma seperated. Thank you.

I am currently developing a Proxy app that visualizes requests and gives you the ability to plug-and-play like filter and modify requests, which makes it a quite powerful tool for debugging web applications, mobile apps and so on.

Of course, this tool is built using Cocoa and Core Foundation. Interesting enough, Core Foundation brings a Type called “CFHTTPMessage”, which handles a lot of low-level message parsing and processing, and is really handy and quite easy to use.

There is one drawback: CFHTTPMessage is not designed to handle both the order in which the headers arrived in a message and duplicate header fields. The first point is somewhat irrelevant, as the HTTP1.1 standard points out that servers are not supposed to depend on any order in the clients – it’s really only a minor issue. The latter point is complicated. HTTP relies on duplicate headers a lot, consider this HTTP request ( Real life!! )


POST /wp-admin/admin-ajax.php HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept-Charset: ISO-8859-1
Accept-Charset: utf-8;q=0.7
Accept-Charset: *;q=0.3
Accept-Encoding: gzip,deflate,sdch
Content-Type: application/x-www-form-urlencoded
Origin: http://momo.brauchtman.net
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1
Cookie: word
Cookie: wordpress_xxxxxxxx
Cookie: wordpress_logged_in_xxxxxx; httponly; expires=Tue
Referer: http://momo.brauchtman.net/wp-admin/post-new.php
Host: momo.brauchtman.net
Accept-Language: de-DE
Accept-Language: de;q=0.8
Accept-Language: en-US;q=0.6
Accept-Language: en;q=0.4
Accept: */*
Content-Length: 1160

There are quite some duplicates here. So the clever guys at Cupertino just forgot to handle duplicate headers at all? No, not really. But they just didn’t document what they are doing with it, which isn’t that clever at all, but fortunately, the sources are available here. So after a bit of digging, I found out that duplicates are simply appended to a former header with same key, which sucks – the delimiter used is a simple comma, which is quite regularly found in header values, which makes splitting an art for itself.

RandomAccessFile weirdness explained, no buffers, just pain.

Ola! I’ve been coding a new android keyboard lately, a project for my university degree but also : for fun. While I was doing this, specially while implementing a file-based dictionary containing frequency information of all words stored in there, I stumbled upon a very, very weird Java-behavior.

Let’s explain it. Using RandomAccessFile, you have both Interfaces, DataInput and DataOutput ( and Closeable ) at your service. Wonderful, I thought, and started to use them. Well, two weeks and endless debugging sessions later I figured out why nothing worked the way it should: RandomAccessFile.

This little class is unable to provide the most simple functionality: Write a byte value, say 42, to a certain position, say 11223 in a file, then seek back to 11223, read a byte value, and make sure it’s the same. The reason for this odd, strange, undesired, undocumented feature? No shared buffers. In fact, no buffers, just for the explicit read and write operations ( they are mapped to native methods anyway ). So basically, everything should work fine in an unbuffered environment, with an operating system directly writing everything down on file.

Because virtually no operating system works unbuffered, RandomAccessFile doesn’t work. The working workaround is to sleep for some time or something alike.

By the way, I was only able to figure it out by looking at the openJDK source, an excellent source in case you’re wondering about some Java behaviour. And thanks to Marc Seeger for his help!

A real open-source, usable, photoshop replacement?

Good morning, folks! Now I’m back in germany, still busy getting used to all this luxury again, including my Ubuntu-desktop. I also have Windows installed, but for some reason i prefer Ubuntu. Anyway, I’m really missing a real, free and usable Photoshop replacement here. For me, GIMP is just a nice little tool enabling one of small edits, but not as sophisticated and well-designed as Photoshop. This is sad because it’s still a big point for many web-doing people not to switch to an open system. And there are plenty of examples where open software can beat the original, look at Open Office.

I’m convinced that there are enough people to start up a project dedicated to building a graphics suite, open, based on already existing tools. Have you heard of Scribus, a great publishing tool for Linux, free? Amazin software, and I dare to speculate that GIMPs codebase isn’t bad, it’s just some frontend stuff that is. By creating a cool team and spending some time on unifying the user interfaces among these, a great step towards permanent switching would be made.

Still, what are you’re experiences with GIMP or do you know any other, comparable tools?

Pass-by-reference workaround in Java

As anybody knows, java passes variables by value. and i haven’t found the magic compiler switch to change that. of course, there are problems caused by this restriction, but in most cases, pass-by-value works just fine. 

If you end up in a situation where it’d come in handy to have a pass-by-reference facility, think about the way java is storing objects, and how these objects are then passed-by-value. 

First of all, an object variable contains only a ( typed ) memory address. Thus, comparing e.g. two strings for equality will yield only true if they are in fact the same strings, same applies for all other types. And this is the value. So in fact you pass a ( kind of ) pointer to a function, enabling to manipulate the original object, as the object is not cloned or something else.

Its also common to encapsulate basic types like int into a object just for the sake of manipulating it in some methods.

So let’s break it down to a simple example:

class MInt {
	int x;
}

public void inc(MInt what){
	what.x++;
}

This will result in the int x of a MInt object being indeed incremented. Let’s imagine calling a increase method with a plain integer. This will change just nothing, at least not outside the methods scope.