Skip to content

Instantly share code, notes, and snippets.

View mtourne's full-sized avatar

Matthieu Tourne mtourne

View GitHub Profile
@mtourne
mtourne / sql-parser.py
Created April 11, 2013 08:13
very slow sql parser in python -- ll(k) recursive decent parser
import logging
import pyparsing as pyp
import re
try:
from ..pylib import db
except (ImportError, ValueError):
from pylib import db
@mtourne
mtourne / gist:5266024
Created March 28, 2013 19:19
Nginx clearing all headers
/* clear all the headers */
ngx_memzero(&r->headers_in, sizeof (ngx_http_headers_in));
/* reinitialize headers */
r->headers_in.content_length_n = -1;
r->headers_in.keep_alive_n = -1;
/* TODO (mtourne): init to the size of the table passed in param */
if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
sizeof(ngx_table_elt_t))
-- list of headers where tables are allowed
local ALLOWED_MULTI_HEADERS = {
["cookie"] = true,
}
local function sanitize_req_headers(req_headers)
for k, v in pairs(req_headers) do
if type(v) == "table" then
if not ALLOWED_MULTI_HEADERS[k] then
local timer = { }
-- shared dictionary
local timer_queue = ngx.shared.timer_queue
local mt = { __index = timer }
local floor = math.floor
local ngx_now = ngx.now
@mtourne
mtourne / multipart.lua
Created December 12, 2012 20:31
HTTP Multipart Encoded Body reader for ngx_lua
-- Copyright (C) 2012 Zhang "agentzh" Yichun (章亦春)
-- Copyright (C) 2012 Matthieu Tourne
module("multipart", package.seeall)
local STATE_BEGIN = 1
local STATE_READING_HEADER = 2
local STATE_READING_BODY = 3
local STATE_ERROR = 4
@mtourne
mtourne / gist:4219847
Created December 5, 2012 21:56
Sample conf for Nginx + Lua
http {
# Lua configuration
lua_package_path "$prefix/?.lua;;";
lua_shared_dict log_dict 1M;
@mtourne
mtourne / nxg_request_time_var.c
Created December 3, 2012 20:31
Expose a $request_time in msec to Nginx core
/* inspired from ngx_http_log_module.c:ngx_http_log_request_time() */
static ngx_int_t
ngx_http_request_time(ngx_http_request_t *r,
ngx_http_variable_value_t *v,
uintptr_t data) {
ngx_time_t *tp;
ngx_msec_int_t ms;
tp = ngx_timeofday();
@mtourne
mtourne / kv_store.lua
Created November 30, 2012 00:24
Restful key value store in ngx_lua
-- Simple KV store in Ngx+Lua
local SHARED_MEM = ngx.shared.mem
local MAX_ARGS = 10
local POST_CHUNK_SIZE = 4096
-- Max size for POST == 1MB
local MAX_POST_SIZE = 1024 * 1024
local DEBUG = true
@mtourne
mtourne / gist:4113700
Created November 19, 2012 20:33
Unified diff of spdy.52 and spdy.53 patches Delta.
diff --git a/src/http/ngx_http_spdy.c b/src/http/ngx_http_spdy.c
index 85b2620..7c55ca7 100644
--- a/src/http/ngx_http_spdy.c
+++ b/src/http/ngx_http_spdy.c
@@ -268,6 +268,8 @@ ngx_http_init_spdy(ngx_event_t *rev)
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"init spdy request");
+ c->log->action = "spdy connection";
+
@mtourne
mtourne / spdy_alternate_protocol.lua
Created November 19, 2012 17:46
Use Alternate-Protocol header to advertise SPDY to clients not using it already.
-- the nginx variable $http_x_spdy_protocol contains the value for X-SPDY-Protocol
local SPDY_REQ_HEADER_VAR='http_x_spdy_protocol'
local ALTERNATE_HEADER='Alternate-Protocol'
local ALTERNATE_VALUE='443:npn-spdy/2'
local function is_spdy_on()
local spdy_version = tonumber(ngx.var[SPDY_REQ_HEADER_VAR]) or 0
ngx.log(ngx.DEBUG, 'SPDY Version: ', spdy_version)