playing with Crystal Programming Language

I’ve started experimenting with the Crystal Programming Language. It’s a nice and clean language with syntax similar to Ruby, but compiled to fast native code, and a lot of clever ideas, like union types and seamless C integration The project is still in early stages, but it’s promising. Just to see how easy, I ported a small python library to Crystal, you can find it on https://github.com/ilmanzo/spark. I hope to find the time to improve on it! ...

October 11, 2016 · Andrea Manzini

a simple HTTP rewriting proxy

This is an example of using goproxy, a fast and robust multithread proxy engine to develop an HTTP proxy that rewrites content on the fly, with multiple search and substitutions. It can be useful for debugging and other less noble (but useful) purposes … // rewriting_proxy project main.go package main import ( "bytes" "flag" "io/ioutil" "log" "net/http" "github.com/elazarl/goproxy" ) var replacements = []struct { from []byte to []byte }{ {[]byte("#e8ecec"), []byte("Red")}, // ugly colors!! {[]byte("Comic Sans MS"), []byte("Lucida Sans Unicode")}, // for eyes sanity {[]byte("Java "), []byte("Golang ")}, // just joking } func myHandler(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { readBody, err := ioutil.ReadAll(resp.Body) if err != nil { //TODO handle read error gracefully return resp } resp.Body.Close() for _, elem := range replacements { readBody = bytes.Replace(readBody, elem.from, elem.to, -1) } resp.Body = ioutil.NopCloser(bytes.NewReader(readBody)) return resp } func main() { verbose := flag.Bool("v", true, "should every proxy request be logged to stdout") proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose proxy.OnResponse().DoFunc(myHandler) log.Fatal(http.ListenAndServe(":8081", proxy)) }...

August 12, 2016 · Andrea Manzini

wrapping c plus plus classes in Python

This is a quick and dirty way to interface C++ code with Python, translating one or more C++ classes in Python objects. First, we need some c++ sample code: //myclass.h #ifndef MYCLASS_H #define MYCLASS_H #include <string> using namespace std; namespace pets { class Dog { public: Dog(string name, int age); virtual ~Dog(); string talk(); protected: string m_name; int m_age; }; }...

August 8, 2016 · Andrea Manzini

il linguaggio Lua: parte 2

segue dalla prima parte Moonwalking: Tipi di dato Nello scorsa puntata abbiamo utilizzato due degli otto tipi disponibili: i numeri e le stringhe. Per semplicita’, Lua non distingue tra interi e floating point: tutti i valori numerici sono conservati come double, cioe’ in virgola mobile a doppia precisione. Nel caso la CPU non disponesse di unita’ FPU, è possibile cambiare una riga nel sorgente (per l’esattezza, #define LUA_NUMBER in lua.h) e ricompilare; questo si fa tipicamente nei sistemi embedded con processori a basse prestazioni. Le stringhe posso essere delimitate da apici singoli o doppi, nel qual caso vengono espanse le usuali sequenze di escape come \b e \n; usando invece i delimitatori [[ ]], possiamo scrivere stringhe su piu’ righe e disattivare l’interpolazione. Vediamo un paio di esempi, sfruttando l’opzione -e per eseguire codice da riga di comando: ...

May 24, 2016 · Andrea Manzini