From ecaf1bd47ef4a363cbb0fc36b5fc68b5abc01ad0 Mon Sep 17 00:00:00 2001 From: Romain Beauxis Date: Sat, 20 Jul 2013 13:06:34 -0500 Subject: [PATCH] Move examples to webcast repo, cleanup some Printf.printf, accept only webcast protocol. --- src/tools/harbor.camlp4 | 6 +- src/tools/harbor.ml | 5 + test/Makefile | 19 --- test/README | 30 ----- test/lame.html | 93 --------------- test/old/httpserver.ml | 60 ---------- test/old/test.html | 57 --------- test/old/wserver.ml | 249 ---------------------------------------- test/raw.html | 89 -------------- test/websocket.liq | 14 --- 10 files changed, 10 insertions(+), 612 deletions(-) delete mode 100644 test/Makefile delete mode 100644 test/README delete mode 100644 test/lame.html delete mode 100644 test/old/httpserver.ml delete mode 100644 test/old/test.html delete mode 100644 test/old/wserver.ml delete mode 100644 test/raw.html delete mode 100755 test/websocket.liq diff --git a/src/tools/harbor.camlp4 b/src/tools/harbor.camlp4 index b5807dec07..ba8f0f11e1 100644 --- a/src/tools/harbor.camlp4 +++ b/src/tools/harbor.camlp4 @@ -661,7 +661,11 @@ let handle_client ~port ~icy h = in let hprotocol = try - if hmethod = `Get && assoc_uppercase "UPGRADE" headers <> "websocket" then raise Exit; + if hmethod = `Get && + assoc_uppercase "UPGRADE" headers <> "websocket" then + raise Exit; + if assoc_uppercase "SEC-WEBSOCKET-PROTOCOL" headers <> "webcast" then + raise Exit; `Websocket with | _ -> hprotocol diff --git a/src/tools/harbor.ml b/src/tools/harbor.ml index 176161ea48..b99d4b04e5 100644 --- a/src/tools/harbor.ml +++ b/src/tools/harbor.ml @@ -634,6 +634,11 @@ let handle_client ~port ~icy h = (* Read and process lines *) ((assoc_uppercase "UPGRADE" headers) <> "websocket") then raise Exit else (); + if + (assoc_uppercase "SEC-WEBSOCKET-PROTOCOL" headers) <> + "webcast" + then raise Exit + else (); `Websocket) with | _ -> hprotocol in diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index 274846cb36..0000000000 --- a/test/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -all: - -server: - python -m SimpleHTTPServer - - -wserver: wserver.ml - ocamlopt.opt -annot -o wserver unix.cmxa -I +threads threads.cmxa str.cmxa -I +cryptokit nums.cmxa cryptokit.cmxa -I +pulseaudio pulseaudio.cmxa wserver.ml - -httpserver: httpserver.ml - ocamlopt.opt -annot -o httpserver unix.cmxa -I +threads threads.cmxa str.cmxa httpserver.ml - -lame: - @if [ ! -d libmp3lame-js ]; then git clone https://github.com/akrennmair/libmp3lame-js.git; fi - -upload: - rsync -avz lame.html wpps:public_html/tmp/ - -.PHONY: lame diff --git a/test/README b/test/README deleted file mode 100644 index e55e3976af..0000000000 --- a/test/README +++ /dev/null @@ -1,30 +0,0 @@ -In order to test the websocket implementation in Liquidsoap, you should follow -those steps: - -1. Lauch the a webserver for the current directory: - - python -m SimpleHTTPServer - -2. Launch the Liquidsoap client - - ./websocket.liq - -3. Go to test webpage http://localhost:8000/raw.html on your favorite browser - (currently this has only be tested with chrome). - -4. Push the Record or Play button on the webpage and you should hear sound! - - -In order to test Lame mp3 encoding you should do: - -1. Checkout the lame git repository (only once) - - make lame - -2. webserver - -3. Liquidsoap client - -4. http://localhost:8000/lame.html - -5. Have fun. \ No newline at end of file diff --git a/test/lame.html b/test/lame.html deleted file mode 100644 index 1b97d46257..0000000000 --- a/test/lame.html +++ /dev/null @@ -1,93 +0,0 @@ - - -Lame Test - - - -

Lame Test

- - - -
- diff --git a/test/old/httpserver.ml b/test/old/httpserver.ml deleted file mode 100644 index c27677dfed..0000000000 --- a/test/old/httpserver.ml +++ /dev/null @@ -1,60 +0,0 @@ -(* Simple webserver for testing... *) - -let port = 8080 - -let file () = - let buflen = 1024 in - let buf = String.create buflen in - let ans = ref "" in - let ic = open_in "lame.html" in - try - while true do - let n = input ic buf 0 buflen in - if n = 0 then raise End_of_file; - ans := !ans ^ String.sub buf 0 n - done; - assert false - with - | End_of_file -> close_in ic; !ans - -let handle s = - let buflen = 1024 in - let buf = String.create buflen in - let headers = - let n = Unix.read s buf 0 buflen in - String.sub buf 0 n - in - Printf.printf "*** Headers ***\n%s\n\n%!" headers; - let f = file () in - Printf.printf "Read file\n%!"; - let ans = Printf.sprintf "HTTP/1.1 200 OK\r\nDate: Mon, 23 May 2005 22:38:34 GMT\r\nServer: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\nLast-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s" (String.length f) f in - Printf.printf "*** Answer ***\n%s\n\n" ans; - Unix.write s ans 0 (String.length ans) - -let () = - Printf.printf "Creating server\n%!"; - let sockaddr = Unix.ADDR_INET(Unix.inet_addr_any, port) in - let domain = - match sockaddr with - | Unix.ADDR_UNIX _ -> Unix.PF_UNIX - | Unix.ADDR_INET (_, _) -> Unix.PF_INET - in - let sock = Unix.socket domain Unix.SOCK_STREAM 0 in - let handle_connexion (s,caller) = - Printf.printf "Handle connection.\n%!"; - let inet_addr_of_sockaddr = function - | Unix.ADDR_INET (n, _) -> n - | Unix.ADDR_UNIX _ -> Unix.inet_addr_any - in - let inet_addr = inet_addr_of_sockaddr caller in - Printf.printf "Openning connection for %s.\n%!" (Unix.string_of_inet_addr inet_addr); - handle s - in - Unix.setsockopt sock Unix.SO_REUSEADDR true; - Unix.bind sock sockaddr; - Unix.listen sock 100; - Printf.printf "Listening for connections.\n%!"; - while true do - let (s, caller) = Unix.accept sock in - ignore (Thread.create handle_connexion (s, caller)); - done diff --git a/test/old/test.html b/test/old/test.html deleted file mode 100644 index 5488a8a026..0000000000 --- a/test/old/test.html +++ /dev/null @@ -1,57 +0,0 @@ - - -WebSocket Test - - -

WebSocket Test

-
- diff --git a/test/old/wserver.ml b/test/old/wserver.ml deleted file mode 100644 index 449aae1c26..0000000000 --- a/test/old/wserver.ml +++ /dev/null @@ -1,249 +0,0 @@ -(* See http://tools.ietf.org/html/rfc6455 *) - -let port = 1234 - -module List = struct - include List - - let may_map f l = - let rec aux = function - | x::t -> - ( - match f x with - | Some x -> x::(aux t) - | None -> aux t - ) - | [] -> [] - in - aux l -end - -(** Base 64 encoding. *) -let encode64 s = - let digit = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" - in - let extra = String.length s mod 3 in - let s = match extra with 1 -> s ^ "\000\000" | 2 -> s ^ "\000" | _ -> s in - let n = String.length s in - let dst = String.create (4 * (n/3)) in - for i = 0 to n/3 - 1 do - let (:=) j v = dst.[i*4+j] <- digit.[v] in - let c j = int_of_char s.[i*3+j] in - let c0 = c 0 and c1 = c 1 and c2 = c 2 in - 0 := c0 lsr 2 ; - 1 := ((c0 lsl 4) land 63) lor (c1 lsr 4) ; - 2 := ((c1 lsl 2) land 63) lor (c2 lsr 6) ; - 3 := c2 land 63 - done ; - if extra = 1 then begin - dst.[4*(n/3)-2] <- '=' ; - dst.[4*(n/3)-1] <- '=' - end else if extra = 2 then - dst.[4*(n/3)-1] <- '=' ; - dst - -let wsa wsk = - let wsa = wsk ^ "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" in - Printf.printf "wsa: %s\n%!" wsa; - let sha1 = Cryptokit.Hash.sha1 () in - sha1#add_string wsa; - let wsa = sha1#result in - for i = 0 to String.length wsa - 1 do - Printf.printf "%x" (int_of_char wsa.[i]) - done; - Printf.printf "\n%!"; - (* TODO: the following returns a truncated answer *) - (* let b64 = Cryptokit.Base64.encode_compact_pad () in *) - (* b64#put_string wsa; *) - (* let wsa = b64#get_string in *) - let wsa = encode64 wsa in - wsa - -let () = - Printf.printf "test WSA: %s\n%!" (wsa "x3JJHMbDL1EzLkh9GBhXDw==") - -let frame s = - let read_char () = - let c = String.create 1 in - assert (Unix.read s c 0 1 = 1); - c.[0] - in - let read_byte () = - let c = int_of_char (read_char ()) in - (* Printf.printf "byte: %d ('%c')\n%!" c (char_of_int c); *) - c - in - let read_short () = - let c1 = read_byte () in - let c2 = read_byte () in - c1 lsl 8 + c2 - in - let read_long () = - let c1 = read_byte () in - let c2 = read_byte () in - let c3 = read_byte () in - let c4 = read_byte () in - c1 lsl 24 + c2 lsl 16 + c3 lsl 8 + c4 - in - let msb c = c land 128 <> 0 in - let read_len () = - let len = ref 0 in - let loop = ref true in - while !loop do - let c = read_byte () in - len := !len * 128 + c land 127; - loop := msb c - done; - !len - in - (* - let t = read_byte () in - Printf.printf "type: %d\n%!" t; - Printf.printf "msb: %B\n%!" (msb t); - (* TODO: raw data otherwise *) - assert (msb t); - let len = read_len () in - Printf.printf "length: %d\n%!" len; - let buf = String.create len in - assert (Unix.read s buf 0 len = len); - Printf.printf "read: %s\n%!" buf - *) - let c = read_byte () in - let fin = c land 0b10000000 <> 0 in - let rsv1 = c land 0b1000000 <> 0 in - let rsv2 = c land 0b100000 <> 0 in - let rsv3 = c land 0b10000 <> 0 in - let opcode = c land 0b1111 in - Printf.printf "fin: %B\n%!" fin; - Printf.printf "rsv: %B %B %B\n%!" rsv1 rsv2 rsv3; - Printf.printf "op: 0x%x\n%!" opcode; - let c = read_byte () in - let mask = c land 0b10000000 <> 0 in - let length = c land 0b1111111 in - Printf.printf "mask: %B\n%!" mask; - let length = - if length = 126 then - read_short () - else if length = 127 then - read_long() - else - length - in - Printf.printf "length: %d\n%!" length; - let masking_key = - if mask then - let key = String.create 4 in - for i = 0 to 3 do - key.[i] <- read_char() - done; - key - else - "" - in - let unmask key s = - if key <> "" then - for i = 0 to String.length s - 1 do - let c = int_of_char s.[i] in - let k = int_of_char key.[i mod 4] in - let c = c lxor k in - let c = char_of_int c in - s.[i] <- c - done - in - (* Printf.printf "masking key: %s\n%!" masking_key; *) - let data = String.create length in - assert (Unix.read s data 0 length = length); - unmask masking_key data; - (* Printf.printf "data: %s\n%!" data; *) - data - -let handle s = - let buflen = 1024 in - let buf = String.create buflen in - let headers = - let n = Unix.read s buf 0 buflen in - String.sub buf 0 n - in - Printf.printf "*** Headers:\n%s\n%!" headers; - let headers = Str.split (Str.regexp "\r\n") headers in - List.iter (fun h -> Printf.printf "header: '%s'\n%!" h) headers; - let headers = - let re = Str.regexp "^\\(.*\\): \\(.*\\)$" in - List.may_map (fun h -> - if Str.string_match re h 0 then - let l = Str.matched_group 1 h in - let v = Str.matched_group 2 h in - Some (l,v) - else - None) headers - in - List.iter (fun (l,v) -> Printf.printf "header: %s / %s\n%!" l v) headers; - let wsk = List.assoc "Sec-WebSocket-Key" headers in - let wsa = wsa wsk in - let ans = Printf.sprintf "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n\r\n" wsa in - Printf.printf "answer:\n%s\n%!" ans; - assert (Unix.write s ans 0 (String.length ans) = String.length ans); - let oc = open_out "wserver.dump" in - let pa = - let sample = - { Pulseaudio. - sample_format = Pulseaudio.Sample_format_float32le; - sample_rate = 44100; - sample_chans = 2; - } - in - Pulseaudio.Simple.create ~client_name:"ocamlsynth" ~dir:Pulseaudio.Dir_playback ~stream_name:"websocket" ~sample () - in - while true do - (* let n = Unix.read s buf 0 buflen in *) - (* Printf.printf "Received:\n%s\n%!" (String.sub buf 0 buflen) *) - let data = frame s in - Printf.printf "data len: %d\n%!" (String.length data); - output_string oc data; - flush oc; - - (* Pulseaudio part *) - let fbuflen = String.length data / 4 in - let fbuf = - Array.init fbuflen - (fun n -> - let ans = ref Int32.zero in - for i = 3 downto 0 do - ans := Int32.shift_left !ans 8; - ans := Int32.add !ans (Int32.of_int (int_of_char data.[4*n+i])) - done; - Int32.float_of_bits !ans - ) - in - Pulseaudio.Simple.write pa [|fbuf;fbuf|] 0 fbuflen; - done - -let () = - Printf.printf "Creating server\n%!"; - let sockaddr = Unix.ADDR_INET(Unix.inet_addr_any, port) in - let domain = - match sockaddr with - | Unix.ADDR_UNIX _ -> Unix.PF_UNIX - | Unix.ADDR_INET (_, _) -> Unix.PF_INET - in - let sock = Unix.socket domain Unix.SOCK_STREAM 0 in - let handle_connexion (s,caller) = - Printf.printf "Handle connection.\n%!"; - let inet_addr_of_sockaddr = function - | Unix.ADDR_INET (n, _) -> n - | Unix.ADDR_UNIX _ -> Unix.inet_addr_any - in - let inet_addr = inet_addr_of_sockaddr caller in - Printf.printf "Openning connection for %s.\n%!" (Unix.string_of_inet_addr inet_addr); - handle s - in - Unix.setsockopt sock Unix.SO_REUSEADDR true; - Unix.bind sock sockaddr; - Unix.listen sock 100; - Printf.printf "Listening for connections.\n%!"; - - while true do - let (s, caller) = Unix.accept sock in - ignore (Thread.create handle_connexion (s, caller)); - done diff --git a/test/raw.html b/test/raw.html deleted file mode 100644 index a0ad82102b..0000000000 --- a/test/raw.html +++ /dev/null @@ -1,89 +0,0 @@ - - -Lame Test - - -

Lame Test

- - - -
- diff --git a/test/websocket.liq b/test/websocket.liq deleted file mode 100755 index e4d053bc64..0000000000 --- a/test/websocket.liq +++ /dev/null @@ -1,14 +0,0 @@ -#!../src/liquidsoap ../scripts/utils.liq - -set("log.file",false) -set("log.stdout",true) -set("log.level",5) -set("server.telnet",false) -set("request.metadata_decoders.duration",true) - -s = input.harbor(port=1234, "/mount") -#s = input.harbor(port=8080, "/foo") -s = audio_to_stereo(s) -s = mksafe(s) - -output.pulseaudio(s)