Author Archive for frankdu

Deploy OAuth provider with mod_wsgi

Recently I deploy an OAuth provider with apache/mod_wsgi. Everything works fine on django dev server locally, but is broken onto apache server. After analyzing the logs, it shows that the HTTP header ‘Authroization’ is dropped by default.

To pass thru OAuth parameters to mod_wsgi applications, you need to use the WSGIPassAuthorization directive. An example is like below, at the same level of ServerName directive:

Bear in mind that the ‘Authorization’ header will be passed thru and renamed to ‘HTTP_AUTHORIZATION’. You need to do such modification if you use python-oauth2 like me. I plan to submit a patch, in order to contribute and learn about git as well, LOL.

References

  1. WSGIPassAuthorization Directive
  2. mod_wsgi: Access Control Mechanisms
  3. My patch for python-oauth2

Many profilers, and I need at least one

Performance improvement is an ongoing task, always. So a profiler is a good friend. For me, the very basic tool is timing and print functions, lol. Yes, they are. In one of language tool, it helps to identify the parsing phase takes 60~80% of the time. The granulatiry is coarse (not down to function level), but very useful. Later I introduced multithreading for parsing. It’s very useful to run one parsing thread on each CPU core.

This week, I tried 2 C++ profiler. ‘Very Sleepy’ and VS profiler. I like ‘Very Sleepy’. It helps to identify a hash table implementation in a runtime module. It is mostly from key collision, and we got significant performance improvement after tuning the codes.

For Java, last year I tried eclipse TPTP and yourkit. TPTP sucks. Yes, you wasted hours and lose patience before a running finished. Normally it takes 30+ seconds for each running. Maybe TPTP is not good at profiling a recursive-descedent parser. Then I tried yourkit and quickly identified which parser rule was causing the problem. After the fix, the time dropped from 1200seconds to ~4 seconds in doing 22 nested if-else-if-else statements. Pretty exciting profiling experience.

4 years ago we profiled a C# web app. I forgot which profiler we used. Finally we improved the performance by trade-off in UX/speed + more offline computing.

Haven’t got experience with profiler in other languages. But so many profilers, I need at least one. :)

Another Way of Javascript Method Overloading

In John Resig post, he presented a subtle method to support method overloading. It’s very useful when you have method overloading and each method behaves differently.

However, it adds overhead by wrap up existing methods inside a new function. For performance consideration, we check the length of arguments to execute different logics. It’s a more efficient and simple way to do the overloading. Examples:

Mercurial: How to ignore everything in a folder?

I have been looking for this for a while. It’s pretty simple. Let’s say you have project A, and want to ignore 2 folders: bin and output.

1. In your project folder, edit the file ‘.hgignore’, or create it if non-existing.
2. Add the following lines:

That’s it. You are all set.

Implement Unsigned Right Shift (>>>) if the Operator is Unsupported

If you are Java/Javascript/AS3 developer, you know what the >>> operator does.

It is called unsigned right shift, or logical right shift. Check the definition in wikipedia.

However, C# and C++ don’t have such operator for signed integers, though it is available for unsigned integers. Here is one of my implementation. Hope it helps if you looking for such code:

Get the real client IP address, when you use a reverse proxy

Let’s say you are running ASP.NET, PHP, JSP, or Python web. If the code is visited via reverse proxy, then the client IP address is the proxy IP with regular detecting routue. To fix the issue, let’s first take a look at 3 HTTP headers added by proxies:

X-Forwarded-For
The IP address(s) of the client. If there are multiple proxies, you see multiple IP addresses, separated by comma. For example:

X-Forwarded-For: client1, proxy1, proxy2

X-Forwarded-Host
The original host requested by the client in the Host HTTP request header.

X-Forwarded-Server
The hostname of the proxy server.

———————————————

The Solution:

It’s pretty straightforward. The flow is:

  1. Check if there is X-Forwarded-For in http headers.
  2. If yes, get the string, and split it by comma. Then get the first piece.
  3. If no, fall back on regular way of reading IP address.

That’s it.

Base64 Encoding in Android 2.1 or Earlier

Base64 is common used. Now Google added a utility class android.util.Base64. Unfortunately it is only available in Android 2.2 and later.

So, you need just a little bit work to use it with Android 2.1 or ealier. Yes, just copy and past the source of Base64 into your project. Make sure to preserve the Apache 2.0 license section, in order to conform to the license. The source code is provided at the end of this post, for your reference.

My disclaimer: I am not responsible for any result in doing the practice described hereby. Read the license if you need.

Now let’s see example usage:

The output is:

Encoded: dGhpcyBpcyBhIG1lc3NhZ2UgYnkgZnJhbmsu

Decoded: this is a message by frank.

Let’s try decode the message in python:

You will get the same input message htere. Now you are ready to send your data to anywhere, like app engine, you know!


The android.util.Base64 source

Resolve ambiguities in antlr v3

When there is ambiguities in writing antlr grammar, there are different ways to solve it.

1. Simply avoid them. This is the ideal way when it’s possible.

2. Turn on backtracking, which is most inefficient way.

3. Use left factoring.

4. Use syntax predicates.

For some samples, check it out at http://www.antlr.org/wiki/display/ANTLR3/How+to+remove+global+backtracking+from+your+grammar

The format of sytnax predicate is close to semantic predicates. There are 3 types of them:

1. Validating semantic predicate, to place constraints on rules. For example
data : ( b += BYTE )+ {$b.size() <= 4}? ;
2. Gated semantic predicate, to turn on some alternatives under runtime condition. For example:
stat: ifState | {allowAssert}?=> assertStat ;
3. Disambiguating semantic predicate, to choose alternatives based on semantic context. For example:
stat: keyIf ID stat | keyCall ID ‘;’ ;
keyIf: {input.LT(1).getText().equals(“if”)}? ID ;
keyCall: {input.LT(1).getText().equals(“call”)}? ID ;