Created
March 18, 2010 21:48
-
-
Save baris/336935 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Printf | |
open Unix | |
let http_get server path = | |
let server_port = 80 | |
and server_addr = | |
try (gethostbyname server).h_addr_list.(0) | |
with Not_found -> exit (-1) in | |
let buffer_size = 1024 in | |
let buffer = String.create buffer_size in | |
let get_command = String.concat "" ["GET "; path; " HTTP/1.0\r\n"; "Host: "; server; "\r\n\r\n"] in | |
let get_command_len = String.length get_command in | |
let sock = socket PF_INET SOCK_STREAM 0 in | |
connect sock (ADDR_INET(server_addr, server_port)); | |
ignore (write sock get_command 0 get_command_len); | |
let rec read_all str = | |
match read sock buffer 0 buffer_size with | |
0 -> str | |
| n -> read_all (String.concat "" [str; (String.sub buffer 0 n)]) in | |
read_all "";; | |
let http_get_data str = | |
let rec get_data str index = | |
try | |
let len = String.length str in | |
let n = String.index_from str index '\n' in | |
let r = String.index_from str n '\r' in | |
if r = index + 2 then | |
String.sub str r (len - r) | |
else | |
get_data str r | |
with Not_found -> "" in | |
get_data str 0 | |
let github x = | |
let str = http_get_data (http_get "github.com" x) in | |
printf "%s\n\n" str; | |
let json = Json_io.json_of_string str in | |
printf "%s\n" (Json_io.string_of_json json) | |
let () = | |
github "/api/v2/json/commits/list/baris/BabyFingers/master"; | |
github "/api/v2/json/commits/list/baris/pyaspects/master"; | |
github "/api/v2/json/user/show/baris" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: get | |
get: get.ml | |
ocamlfind ocamlopt -package json-wheel -linkpkg get.ml -o get | |
clean: | |
rm get.cm[xi] get.o get |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment