Check if the connection is secure in django with nginx and gunicorn

I’m serving a django application both through http and https, and since it uses Orbited (which listens to different ports for each type of connection), I needed a way to tell from my templates (from a template context processor to be exact) if the client was using http or https.

Using just request.is_secure wouldn’t work, since my project is served by gunicorn through a nginx proxy_pass, and by default, django can’t tell if its being served on a secure connection.

I don’t know if it’s the best way to do it, but this is how I managed to solve it. First I added the line:

proxy_set_header X-Forwarded-Protocol https;

In the nginx configuration, on the server that uses ssl. Then, I modified the configuration of gunicorn, as suggested in the website, with the setting:

secure_scheme_headers={'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-SSL': 'on'}

Lastly, from my context processor, I checked the request headers to tell if the connection is http or https:

def is_secure(request):
    return ('HTTP_X_FORWARDED_PROTOCOL' in request.META and 
        request.META['HTTP_X_FORWARDED_PROTOCOL'] == 'https')

And that’s it.

C++ is rubbish: some notes

Again, I’ve been forced to use C++. As in all my previous experiences with the language, it’s a major pain in the ass. I came to the conclusion that C++ is a programming language which no one can ever master; no matter how long you use it, after six months off of it you forget everything you knew, because it’s just too big and completely counterintuitive.

Someday I might take the time to explain more profoundly why I hate (I’m using the word hate here) C++. For now I’ll just make some notes.

I’ve been forced many times to use it, and no one could ever give me a good reason why. I believe such reason does not exist. If you want to do low level, system level or whatever you want to call it programming, C will do. While it lacks the facilities of more recent languages (and lisp), making it a doubtful choice for general purpose programming when efficiency is not the primary concern, I found C to be a really simple and clean language. It’s not at all hard to keep the primary feature set and libraries in your head.

C++ on the other hand is huge. You need to google constantly to do the most simple things, and what’s worse, you should do it, because there are a lot of things that just don’t work in the obvious ways. This is specially true when working with files.

All the advantages of object-orientedness quickly fade away when you see yourself dealing over and over with obscure syntax errors on classes that are a hundred lines long way before they do any real work. The real advantage of object oriented programming is the way it helps to think and structure your programs, and you don’t need classes or inheritance to accomplish that.

C++ is so bad that makes Java look good. I don’t like Java. I think it’s too verbose and lacks several features I expect from a high level language, most of its advanced techniques (design patterns, reflection, xml frameworks) being just patches for those absences. But all in all, Java is a language that makes sense. C++ is not.

It’s backwards compatible with C. Seriously Stroustrup? What on earth were you thinking?

The compiler never helps. The error messages never make sense, and in most cases ignoring them is the healthier choice.

C++ Strings suck. They suck so hard that they’re not a built-in type, they’re not even used in the libraries, so you have to use stuff like c_str().

Streams suck. The syntax is weird, they’re just too many of them, I seem to recall there was some diamond inheritance weirdness going on over here… I’d rather stick to C files.

STL sucks. The iterators are error-prone and tedious to use. You have to write a lot to use most of the methods of the containers, even for basic stuff, ending up with code like:

    for (map<string, string>::iterator it = myMap.begin(); it
            != myMap.end(); it++) {
        string key = it->first;
        string value = it->second;
        //...
    }

Don’t even get me started on templates.

Whether you use them or not, namespaces are a pain in the ass. If you do use them, the code gets confusing and annoying to write (and it’s easier to get compiler errors when you forget to specify the namespace). If you don’t use them in your code you still need to add the using namespace std bit everywhere or you get syntax errors that as usual don’t point the problem.

Why are there references? One can’t consistently use either references or pointers, you are forced to mix them in certain contexts [1].

The language is not consistent. An example: you instantiate classes in the stack like:

    MyClass myObject(arg1, arg2);

But what if your class has a constructor that takes no arguments? You might expect it to work like this:

    MyClass myObject();

But no, the correct way is omitting the parenthesis:

    MyClass myObject;

You might find yourself wondering for half an hour why your program won’t compile for things like this.

Methods are not virtual by default. Premature optimization is the root of all evil.

It’s copy-paste oriented. Code duplication is trouble. In C++, the standard way to code a class is to copy all the method prototypes from the .h to the .cpp file, adding the MyClass:: bit. Even with just a couple of getters and setters, theres a whole lot of typing and copying. This is not only tedious, but one of the places where more errors occur. And remember, the compiler speaks its own language.

You can’t refactor. Unit testing frameworks are awkward to use and setup. Creating a class and making it compile is so hard that most of the time you stick to add an if.

[1] http://www.paploo.net/programming/rant_01.shtml