Skip to content

Instantly share code, notes, and snippets.

@elazarl
elazarl / clientDoGzip.go
Created March 16, 2012 15:01
Client.Do will not uncompress response body
package main
// run this file with `go run clientDoGzip.go, and then file tmp.out to verify body is gzipped
import "net/http"
import "io/ioutil"
import "bytes"
import "fmt"
import "log"
import "compress/gzip"
@elazarl
elazarl / partialResp.go
Created March 25, 2012 18:11
A draft code to read a partial response from http server
package main
import ("bufio"
"fmt"
"io"
"net/http"
"net"
"strings")
type readFirstCloseSecond struct {
package main
import (
"io"
"os"
"time"
)
func singLine(line string,w io.Writer) {
time.Sleep(time.Second)
@elazarl
elazarl / INT 13 fail
Last active October 19, 2015 16:29
Boot loader failure
This is a demonstration of how INT 13, ah=42 fails after a while.
Run 'bash debug.sh`, then hit `c` a couple of times, you should
see that INT 13 returns with CF set, which means it had an error.
Why is it happening? The code is pretty simple, all it does is reading
a sector from the disk, and copying it to some place in the memory.
@elazarl
elazarl / tcpsymcon.go
Created June 11, 2013 06:36
This code snippet demonstrate the tcp simultaneous connection feature. No one of the goroutines has a listening socket, yet, when both try to connect to each other simultaneously the connection succeeds. I've yet to understand what is this feature good for, I definitely know what is it not good for: http://golang.org/src/pkg/net/tcpsock_posix.go…
package main
import "fmt"
import "net"
var localhost = net.ParseIP("127.0.0.1")
var port1, port2 = 1220, 1229
func conwrite() {
c1, err := net.DialTCP("tcp", &net.TCPAddr{localhost, port2}, &net.TCPAddr{localhost, port1})
@elazarl
elazarl / NoLoopBackSocket.java
Created June 11, 2013 06:48
This test shows that Java will not allow a socket to connect to itself via TCP's simultaneous connection feature, by setting the source port identical to the destination port.
import org.junit.Test;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
/**
* This tests make sure Java prevents a socket from connecting to itself
* via binding to the same port it connects to
@elazarl
elazarl / poll_dev.c
Created June 18, 2013 14:33
A simple test to determine if your OS allows polling devices, e.g. /dev/null
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
int main() {
int fd = open("/dev/null", O_WRONLY);
struct pollfd pollfds = { fd, POLLOUT, 0 };
if (fd < 0) {
perror("open");
@elazarl
elazarl / bug.go
Created October 7, 2013 21:38
possible bug in Go's code generation
package bug
type returnPanic struct {
err error
}
func F() (err error) {
defer func() {
if _, ok := recover().(returnPanic); !ok {
panic(err)
@elazarl
elazarl / cb.go
Created November 5, 2013 07:21
No backtrace for Go calls across SOs
package main
func Panic() { panic("oooh, no stacktrace") }
func make_bt_longer() {
Panic()
}
func main() {
Completer = func (text string, start, end int) (replacement string, options []string) {
@elazarl
elazarl / POD.h
Created March 6, 2016 14:16
Serializing dictionary to NSObjects, a la Go's json package
#import <Foundation/Foundation.h>
@interface POD : NSObject<NSCopying>
-(nonnull NSString *)description;
-(nonnull NSDictionary *)toDictionary;
+(nonnull id)fromDictionary:(nonnull NSDictionary *)dict;
-(nonnull id)copyWithZone:(nullable NSZone *)zone;
@end