Skip to content

Instantly share code, notes, and snippets.

View perusio's full-sized avatar

António P. P. Almeida perusio

View GitHub Profile
@perusio
perusio / gist:1686457
Created January 27, 2012 01:57
Drupal 7 DBTNG JOIN example
<?php
// Use a JOIN there are aliases.
$select = db_select('url_alias', 'a');
$select->join('node', 'n', 'SUBSTR(a.source, 6) = n.nid');
$pairs = $select->fields('a', array('source', 'alias'))
->orderBy('changed', 'DESC')
->condition('changed', $updated, '>=')
->execute()
->fetchAllKeyed();
@perusio
perusio / memcached_keys_list.rb
Created January 27, 2012 19:09 — forked from stackdump/gist:1479007
List the keys in a given memcached instance running locally
#!/usr/bin/env ruby
require 'net/telnet'
cache_dump_limit = 100
localhost = Net::Telnet::new("Host" => "localhost", "Port" => 11211, "Timeout" => 3)
slab_ids = []
localhost.cmd("String" => "stats items", "Match" => /^END/) do |c|
matches = c.scan(/STAT items:(\d+):/)
slab_ids = matches.flatten.uniq
end
@perusio
perusio / gist:1695505
Created January 28, 2012 19:25
Nginx Hackday Porto Linux SSL for authenticated users
## At the http level
map $http_cookie $is_secure {
default 0;
~SESS 1; # there's a session cookie (use SSL - authenticated user)
}
map $is_secure $not_secure {
1 0;
0 1;
}
@perusio
perusio / gist:1695600
Created January 28, 2012 20:05
Nginx Hackday Porto Linux Limit Requests for Authenticated Users
## At the http level define a connection zone. This is the new post Nginx 1.1.9 syntax that allows multiple zones
## Define two connection zones: arbeit and auth_jail
limit_conn_zone $binary_remote_addr zone=arbeit:10m; # client IP
limit_conn_zone $http_cookie zone=auth_jail:10m; # Cookie header
## Define a map for singling out logged in users.
map $http_cookie $is_authenticated {
default 0;
~SESS 1;
@perusio
perusio / gist:1724301
Created February 2, 2012 16:13
Workaround in PHP cURL for the 100-continue expectation
<?php
// cURL obeys the RFCs as it should. Meaning that for a HTTP/1.1 backend if the POST size is above 1024 bytes
// cURL sends a 'Expect: 100-continue' header. The server acknowledges and sends back the '100' status code.
// cuRL then sends the request body. This is proper behaviour. Nginx supports this header.
// This allows to work around servers that do not support that header.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// We're emptying the 'Expect' header, saying to the server: please accept the body right now.
// Read here: http://pilif.github.com/2007/02/the-return-of-except-100-continue/
@perusio
perusio / gist:1747124
Created February 5, 2012 18:43
Request processing debug log
2012/02/05 18:35:43 [debug] 3727#0: *12698 event timer del: 55: 1328467003701
2012/02/05 18:35:43 [debug] 3727#0: *12698 generic phase: 0
2012/02/05 18:35:43 [debug] 3727#0: *12698 rewrite phase: 1
2012/02/05 18:35:43 [debug] 3727#0: *12698 http script var
2012/02/05 18:35:43 [debug] 3727#0: *12698 http map started
2012/02/05 18:35:43 [debug] 3727#0: *12698 http script var: "curl/7.24.0 (x86_64-pc-linux-gnu) libcurl/7.24.0 OpenSSL/1.0.0g zlib/1.2.3.4 libidn/1.23 libssh2/1.2.8 librtmp/2.3"
2012/02/05 18:35:43 [debug] 3727#0: *12698 http map: "curl/7.24.0 (x86_64-pc-linux-gnu) libcurl/7.24.0 OpenSSL/1.0.0g zlib/1.2.3.4 libidn/1.23 libssh2/1.2.8 librtmp/2.3" "0"
2012/02/05 18:35:43 [debug] 3727#0: *12698 http script var: "0"
2012/02/05 18:35:43 [debug] 3727#0: *12698 http script if
2012/02/05 18:35:43 [debug] 3727#0: *12698 http script if: false
@perusio
perusio / gist:1784254
Created February 9, 2012 23:33
Nginx support for rel="canonical" HTTP headers
## As explained here: http://googlewebmastercentral.blogspot.com/2011/06/supporting-relcanonical-http-headers.html
## This is the add_header incantation to make it work.
add_header Link "<$scheme://$http_host$request_uri>; rel=\"canonical\"";
@perusio
perusio / gist:1794903
Created February 11, 2012 01:17
Echo example for lsyncd
settings = {
logfile = '/tmp/lsyncd.log',
statusFile = '/tmp/lsyncd.stat',
statusInterval = 1,
nodaemon = true
}
echo = {
maxProcesses = 1,
delay = 1,
@perusio
perusio / file_exists.lua
Created February 14, 2012 05:29
Test if a file exists using Lua posix
-- Test if a file exists using Lua posix https://github.com/rrthomas/luaposix.
local posix = require('posix')
-- Test if a file exists.
-- @param fname string
-- filename
-- @return boolean
-- true if file exists, false otherwise
--
@perusio
perusio / file_exists.lua
Created February 14, 2012 05:36
Check if a file exists using nixio.
-- Playing with the nixio.fs library.
local nixiofs = require('nixio.fs')
-- Test if a file exists.
-- @param fname string
-- filename
-- @return boolean
-- true if file exists, false otherwise
--