Wednesday, February 1, 2012

How to grab the http header zippy fast

// Magic header, we reject everything else
#define JSON_TYPE "POST\r\nContent-Type:text/json\n\rContent-Length:"
#define BSON_TYPE "POST\r\nContent-Type:text/bson\n\rContent-Length:"
#define MAGIC_SIZE sizeof(JSON_TYPE)
#define HEADER_SIZE (MAGIC_SIZE+10)
int header_magic(int newfd,int * count) {
    char inbuffer[HEADER_SIZE];
    int rv; int type;int i;
    type = -1;
    rv = read(newfd, inbuffer, HEADER_SIZE);
    if(rv != -1 && rv == HEADER_SIZE) {
     if(!strncmp(inbuffer,JSON_TYPE,MAGIC_SIZE)) type = 1;
     else if(!strncmp(inbuffer,BSON_TYPE,MAGIC_SIZE)) type = 0;
    }
    if(type != -1)
      *count = (int) inbuffer[MAGIC_SIZE];
    return (type);
    }

Screwing with http headers. Here is how to do it, grab the whole chunk, and if it is the magic header, go run. One single magic header gets the bots to jason or bson handlers. Both sides select the same fixed size header. A few instructions in and out.

I hold the record for the least code written!

No comments: