Skip to content

https authentication in nginx

In order not to forget again how to setup http authentication in nginx here is a reminder.

For https, follow these steps:

    $ sudo -s
    # cd /etc/nginx
    # openssl req -new -x509 -nodes -out server.crt -keyout server.key

and add these lines to your server instance:

    server {
        listen 443;
        ssl                  on;
        ssl_certificate      /etc/nginx/server.crt;
        ssl_certificate_key  /etc/nginx/server.key;
    ...

To setup basic authentication, add these lines to the same file:

    location / {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
        # you might also want to deny access based on IP here
        #allow <ip-address>;
        #deny all;
    ...

Finally, to generate /etc/nginx/htpasswd, use this one-liner:

    echo -e "your-username:`perl -le 'print crypt("your-password","salt")'`" > /etc/nginx/htpasswd

Restart nginx and Bob’s your uncle!

Tagged , ,

ulint, Universal Lint

I’ve been using so many code static checkers lately, that I decided to write a wrapper around the ones I use the most.

I called it: Universal Lint, ulint for short.

At the moment, Python, Javascript and Haskell files are supported, but adding new linters/extension is trivial.

Download it and use it!

500 on Youtube on non-existing videos

Now, this is embarassing…

It looks like requesting a non-existent video on Youtube causes a “500 Internal Server Error”!

500

Requesting a non-existing video on Youtube returns 500!

For example:

Surely, a better response would be something like the one returned when  the videoId is misisng: http://www.youtube.com/watch?v=

Seems like predictions became true.

HSGrep benchmarking

Few days ago, I rewrote sgrep in Haskell. I was curious to know how it compares to grep in term of execution speed. In particular, I was interested to verify that hsgrep scales as O(log n), instead of O(n), with n being the size of the file analyzed.

First of all, in order to have similar performance to grep, I had to convert my original program to use Haskell’s bytestrings. You can find the code here.

Testing files are generated with this script.

Here are the results obtained. grep is still faster for smallish files (I haven’t spent too much time tweaking hsgrep), but hsgrep scales much better and it wins for files larger than few megabytes!

Tagged , ,

HSGrep: Sorted Grep in Haskell

As an exercise to learn Haskell, I wrote a specialized grep to work on sorted files. It uses binary search to scan a text file and print all the (consecutive) lines that start with a user defined string.

My program is a rewrite of sgrep in Haskell: I called it HSGrep.

Code is available on github.

Thanks a lot for all your useful suggestions! As soon as possible, I’ll post some benchmarking here. (EDIT: benchmarks now available!)

After downloading the source code, build install and run it with:

$> cabal build
$> cabal install
$> hslint <string> <filename>
Tagged , ,

git pre-commit hook for python and javascript

Following a recent discussion on HN, I decided to share my own git pre-commit hook.

#!/usr/bin/python

import os
import sys
import re
import subprocess

devnull = open(os.devnull, 'w')

def call(cmd):
    p = subprocess.Popen(cmd.split(),
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
    out, err = p.communicate()
    return out.decode('utf-8'), err.decode('utf-8')

def execute(cmd, silent=False):
    if silent:
        params = {
                'stdout': devnull,
                'stderr': devnull,
                }
    else:
        params = {}

    retcode = subprocess.call(cmd.split(), **params)
    return retcode

def exists(cmd):
    return execute('which %s' % cmd, silent=True) == 0

def get_modified(ext):
    modified = re.compile('^(?:M|A).(?P<name>.*\.%s)' % ext)
    out, _ = call('git status --porcelain')
    modifieds = []
    for line in out.splitlines():
        match = modified.match(line.strip())
        if (match):
            modifieds.append(match.group('name'))
    return modifieds

def output(prg, out, err):
    print(' * %s:\n%s\n%s' % (prg, out, err))

def die(msg):
    print(msg)
    sys.exit(1)

def check_python():

    has_pep8 = exists('pep8')
    has_pyflakes = exists('pyflakes')
    if not (has_pep8 or has_pyflakes):
        die('Install PEP8 and PyFlakes!')

    modifieds = get_modified('py')
    rrcode = 0
    for file in modifieds:
        if has_pep8:
            out, err = call('pep8 %s' % file)
            if out or err:
                output('pep8', out, err)
                rrcode = rrcode | 1
        if has_pyflakes:
            retcode = execute('pyflakes %s' % file)
            rrcode = retcode | rrcode

    if rrcode != 0:
        sys.exit(rrcode)

def check_javascript():

    has_jsl = exists('gjslint')
    if not has_jsl:
        die('Install Closure-Lint!')

    modifieds = get_modified('js')
    rrcode = 0
    for file in modifieds:
        out, err = call('gjslint %s' % file)
        if out or err:
            output('gjslint', out, err)
            rrcode = rrcode | 1

    if rrcode != 0:
        sys.exit(rrcode)

def main():
    check_python()
    check_javascript()

if __name__ == '__main__':
    main()

It’s a work-in-progress, so you can find the most updated version here.

To use it, just drop it in your .git/hooks directory. At every git commit, it will run pep8 and pyflakes on .py files, and gjslint on .js files.

Tagged , , , , , ,

Haskell and VIM

I had to tinker quite a bit before finding a decent configuration for vim to edit Haskell files.

Here are the packages and config files I use:

  • Haskell mode: interaction with Haddock, compiler integration, and other general settings.
  • Indentation: proper source code indentation.
  • More configs to enforce good style (no tabs, tabstops, etc.): 
    " Add to .vimrc
    " Need to have .vim/compiler/ghc.vim

    augroup HSK
            au Bufenter *.hs compiler ghc
            autocmd FileType haskell setlocal formatoptions+=t
            autocmd FileType haskell let b:ghc_staticoptions = '-Wall -Werror'
    augroup END

    " Add to .vim/ftplugin/haskell.vim

    " From Good Haskell Style http://urchin.earth.li/~ian/style/haskell.html
    setlocal expandtab
    setlocal tabstop=8
    setlocal shiftwidth=8
    setlocal textwidth=79
    " Add to .vim/syntax/haskell.vim

    " From Good Haskell Style http://urchin.earth.li/~ian/style/haskell.html

    syn cluster hsRegions add=hsImport,hsLineComment,hsBlockComment,hsPragma
    syn cluster hsRegions add=cPreCondit,cCppOut,cCppOut2,cCppSkip
    syn cluster hsRegions add=cIncluded,cDefine,cPreProc,cComment,cCppString

    syn match tab display "\t" containedin=@hsRegions
    hi link tab Error
    syn match trailingWhite display "[[:space:]]\+$" containedin=@hsRegions
    hi link trailingWhite Error

You can also have them bundled all together.

Tagged , ,

they broke delicious

the new redesign of delicious.com broke so many things, that I thought I had to take notes:

  • bookmarklet in Chrome can’t be drag-n-dropped to the toolbar. its javascript code is also hard to copy and paste to create the bookmarklet “manually”.
  • install links for the chrome and firefox extensions redirect to empty pages.
  • documentation for developer is coming soon (!)
  • the top-right menu with the logged-in username and stuff is the most horrible I’ve ever seen: it changes its width on hover, the corners look interrupted (in Chrome, at least — I’m not bothered trying with other browsers)
  • what the hell are stacks? grouping can be done with tags, already, and I don’t want that much space in the page taken by a “social” feature! who needs it?
  • the list of links in the main personal page only shows 8 links on my screen… 8 links?! there’s way too much padding, too whitespaces, too much to scroll… it’s a bookmarking services, it should show you bookmarks in the most friendly way!
  • image: why should I use an image for myself? and if I don’t use one, I got assigned one by default… please…

I’ll add more to the list in the future.

Tagged

http client in haskell

To try to make sense of this, I decided to write a “simple” http client in Haskell in as many styles as I could think of:

import Network.HTTP
import Control.Applicative

url = "http://www.haskell.org/haskellwiki/Haskell"

--  Imperative style
fetch_1 = do
	rsp <- Network.HTTP.simpleHTTP (getRequest url)
	body <- getResponseBody rsp
	return (take 1000 body)

--  With Functors' fmap
fetch_2 = do
	rsp <- Network.HTTP.simpleHTTP (getRequest url)
	fmap (take 1000) (getResponseBody rsp)

--  With Applicative's >>=
fetch_3 = fmap (take 1000) (Network.HTTP.simpleHTTP (getRequest url) >>= getResponseBody)

--  "fmap f x" is the same as "pure f <*> x" (Applicative's law)
fetch_4 = pure (take 1000) <*> (Network.HTTP.simpleHTTP (getRequest url) >>= getResponseBody)

--  "pure f <*> x" is the same as "f <$> x"
fetch_5 = (take 1000) <$> (Network.HTTP.simpleHTTP (getRequest url) >>= getResponseBody)

it’s a long time since I found so many new concepts while studying a new programming language…

Tagged , , ,

pylint.vim

vim + python + pylint is a powerful combination, especially when using this vim plugin.

unfortunately, the script stopped working after a pylint upgrade:

$ pylint --version
pylint 0.24.0,
astng 0.22.0, common 0.56.0
Python 2.7.2 (default, Jun 29 2011, 11:10:00)
[GCC 4.6.1]

you can download the fixed version here!

if interested, the patch looks like this:

diff pylint.vim.orig pylint.vim
69c69
< CompilerSet makeprg=(echo\ '[%]';\ pylint\ -r\ y\ %)
---
> CompilerSet makeprg=(echo\ '[%]';\ pylint\ -r\ y\ --output-format=parseable\ %)
74c74
< CompilerSet efm=%+P[%f],%t:\ %#%l:%m,%Z,%+IYour\ code%m,%Z,%-G%.%#
---
> CompilerSet efm=%f:%l:\ [%t]%m,%f:%l:%m
Tagged , ,