2012-04-29 20:34:52 +00:00
|
|
|
(* Copyright 2012 Tony Garnock-Jones <tonygarnockjones@gmail.com>. *)
|
|
|
|
|
2012-05-01 21:36:38 +00:00
|
|
|
(* This file is part of Hop. *)
|
2012-04-29 20:34:52 +00:00
|
|
|
|
2012-05-01 21:36:38 +00:00
|
|
|
(* Hop is free software: you can redistribute it and/or modify it *)
|
2012-04-29 20:34:52 +00:00
|
|
|
(* under the terms of the GNU General Public License as published by the *)
|
|
|
|
(* Free Software Foundation, either version 3 of the License, or (at your *)
|
|
|
|
(* option) any later version. *)
|
|
|
|
|
2012-05-01 21:36:38 +00:00
|
|
|
(* Hop is distributed in the hope that it will be useful, but *)
|
2012-04-29 20:34:52 +00:00
|
|
|
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
|
|
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
|
|
|
|
(* General Public License for more details. *)
|
|
|
|
|
|
|
|
(* You should have received a copy of the GNU General Public License *)
|
2012-05-01 21:36:38 +00:00
|
|
|
(* along with Hop. If not, see <http://www.gnu.org/licenses/>. *)
|
2012-04-29 20:34:52 +00:00
|
|
|
|
2012-05-07 10:31:59 +00:00
|
|
|
open Lwt
|
2012-05-01 12:06:30 +00:00
|
|
|
open Hof
|
2012-05-10 01:53:00 +00:00
|
|
|
open Datastructures
|
|
|
|
|
|
|
|
let all_sources = ref StringMap.empty
|
2012-05-01 12:06:30 +00:00
|
|
|
|
2012-05-07 10:31:59 +00:00
|
|
|
let rec api_tap_source id r =
|
|
|
|
let mbox = Lwt_mvar.create
|
|
|
|
(Some (Message.subscribe (Sexp.Str (Node.local_container_name()),
|
|
|
|
Sexp.Str "", Sexp.Str "",
|
|
|
|
Sexp.Str "", Sexp.Str ""))) in
|
|
|
|
|
|
|
|
let handle_message n sexp = Lwt_mvar.put mbox (Some sexp) in
|
|
|
|
let n = Node.make "http_tap" handle_message in
|
2012-05-10 01:53:00 +00:00
|
|
|
all_sources := StringMap.add id n !all_sources;
|
2012-04-29 20:34:52 +00:00
|
|
|
|
2012-05-07 10:31:59 +00:00
|
|
|
let shutdown () =
|
2012-05-10 01:53:00 +00:00
|
|
|
all_sources := StringMap.remove id !all_sources;
|
2012-05-07 10:31:59 +00:00
|
|
|
lwt () = Node.unbind_all n in
|
|
|
|
Lwt_mvar.put mbox None
|
2012-04-29 20:34:52 +00:00
|
|
|
in
|
|
|
|
|
2012-05-07 10:31:59 +00:00
|
|
|
let generator yield =
|
|
|
|
let body_counter = ref 0 in
|
|
|
|
let yield_and_count s =
|
|
|
|
body_counter := String.length s + !body_counter;
|
|
|
|
yield s
|
|
|
|
in
|
2012-05-10 01:53:00 +00:00
|
|
|
lwt () = yield_and_count (id ^ ";" ^ String.make 2048 'h' ^ ";") in
|
2012-05-07 10:31:59 +00:00
|
|
|
let rec drain_mbox () =
|
|
|
|
match_lwt Lwt_mvar.take mbox with
|
|
|
|
| None -> return ()
|
|
|
|
| Some sexp ->
|
|
|
|
lwt json = Sexpjson.json_of_sexp sexp in
|
|
|
|
let payload = Json.to_string json in
|
|
|
|
lwt () = yield_and_count (Printf.sprintf "%d;%s;" (String.length payload) payload) in
|
|
|
|
lwt () =
|
|
|
|
if !body_counter >= 131072
|
|
|
|
then shutdown ()
|
|
|
|
else return ()
|
|
|
|
in
|
|
|
|
drain_mbox ()
|
|
|
|
in drain_mbox ()
|
|
|
|
in
|
|
|
|
|
2012-05-10 01:53:00 +00:00
|
|
|
Httpd.resp_generic 200 "Streaming"
|
|
|
|
[Httpd.text_content_type_header;
|
|
|
|
"Access-Control-Allow-Origin", "*"]
|
|
|
|
(Httpd.Variable (Streamutil.stream_generator generator))
|
|
|
|
|> Httpd.add_disable_cache_headers
|
|
|
|
|> Httpd.add_date_header
|
|
|
|
|> Httpd.add_completion_callback shutdown
|
|
|
|
|
|
|
|
let dispatch_message n m =
|
|
|
|
match m with
|
|
|
|
| Message.Post (Sexp.Str name, body, token) ->
|
|
|
|
lwt () = Node.send_ignore' name body in
|
|
|
|
Httpd.resp_generic 202 "Accepted" [] (Httpd.empty_content)
|
|
|
|
| Message.Subscribe (Sexp.Str filter, sink, name, Sexp.Str reply_sink, Sexp.Str reply_name) ->
|
|
|
|
(match_lwt Node.bind (Node.name_of_string filter, n) with
|
|
|
|
| true ->
|
|
|
|
lwt () = Node.post_ignore'
|
|
|
|
reply_sink
|
|
|
|
(Sexp.Str reply_name)
|
|
|
|
(Message.subscribe_ok (Sexp.Str filter))
|
|
|
|
(Sexp.Str "")
|
|
|
|
in
|
|
|
|
Httpd.resp_generic 204 "Bound" [] (Httpd.empty_content)
|
|
|
|
| false ->
|
|
|
|
lwt () = Log.warn "Bind failed" [Sexp.Str filter] in
|
|
|
|
Httpd.http_error_html 409 "Bind failed" [])
|
|
|
|
| Message.Unsubscribe (Sexp.Str token) ->
|
|
|
|
(match_lwt Node.unbind (Node.name_of_string token) with
|
|
|
|
| true ->
|
|
|
|
Httpd.resp_generic 204 "Unbound" [] (Httpd.empty_content)
|
|
|
|
| false ->
|
|
|
|
lwt () = Log.warn "Unbind failed" [Sexp.Str token] in
|
|
|
|
Httpd.http_error_html 409 "Unbind failed" [])
|
|
|
|
| _ ->
|
|
|
|
Httpd.http_error_html 406 "Message not understood" []
|
2012-04-29 20:34:52 +00:00
|
|
|
|
|
|
|
let api_tap_sink irrelevant_id r =
|
2012-05-07 10:31:59 +00:00
|
|
|
lwt content = Httpd.string_of_content r.Httpd.req_body.Httpd.content in
|
|
|
|
lwt params = Httpd.parse_urlencoded content in
|
2012-04-29 20:34:52 +00:00
|
|
|
match Httpd.find_param "metadata.type" params with
|
2012-05-07 10:31:59 +00:00
|
|
|
| Some (Some "send") ->
|
2012-05-10 01:53:00 +00:00
|
|
|
(match Httpd.find_param "metadata.id" params with
|
|
|
|
| Some (Some id) ->
|
|
|
|
(match (try Some (StringMap.find id !all_sources) with Not_found -> None) with
|
|
|
|
| Some n ->
|
|
|
|
(match Httpd.find_param "data" params with
|
|
|
|
| Some (Some data_str) ->
|
|
|
|
lwt data =
|
|
|
|
(try return (Sexpjson.sexp_of_json (Json.of_string data_str))
|
|
|
|
with _ -> Httpd.http_error_html 406 "Bad data parameter" []) in
|
|
|
|
dispatch_message n (Message.message_of_sexp data)
|
|
|
|
| _ -> Httpd.http_error_html 406 "Bad data parameter" [])
|
|
|
|
| None ->
|
|
|
|
Httpd.http_error_html 406 "Tap ID not found" [])
|
|
|
|
| _ ->
|
|
|
|
Httpd.http_error_html 406 "Missing metadata.id" [])
|
2012-05-07 10:31:59 +00:00
|
|
|
| Some (Some "close") ->
|
2012-05-01 13:25:24 +00:00
|
|
|
Httpd.resp_generic_ok [] Httpd.empty_content
|
2012-05-07 10:31:59 +00:00
|
|
|
| _ ->
|
2012-05-01 13:25:24 +00:00
|
|
|
Httpd.http_error_html 406 "Unsupported metadata.type" []
|
2012-04-29 20:34:52 +00:00
|
|
|
|
2012-05-01 12:30:17 +00:00
|
|
|
let api_tap _ id r =
|
2012-04-29 20:34:52 +00:00
|
|
|
match r.Httpd.verb with
|
2012-05-07 10:31:59 +00:00
|
|
|
| "GET" -> api_tap_source id r
|
|
|
|
| "POST" -> api_tap_sink id r
|
|
|
|
| _ -> Httpd.http_error_html 400 "Unsupported tap method" []
|
2012-04-29 20:34:52 +00:00
|
|
|
|
|
|
|
let init () =
|
|
|
|
Ui_main.register_dispatcher ("/_/tap", api_tap)
|