Skip to content
Browse files

[feature] libnet: Added all-methods option to HttpServer.

  • Loading branch information...
1 parent 68b7e50 commit d0d0150827a74fcc157cc7492e90b7b0efbc8230 @nrs135 nrs135 committed with Aqua-Ye
Showing with 40 additions and 26 deletions.
  1. +13 −5 libnet/httpServer.ml
  2. +25 −19 libnet/httpServerCore.proto
  3. +1 −1 libnet/httpServerTypes.ml
  4. +1 −1 libnet/httpServerTypes.mli
View
18 libnet/httpServer.ml
@@ -458,8 +458,8 @@ let handle_put sched runtime _method hr (uri, headers, body) conn k =
server_info = hr.HST.hr_server_info; is_multipart = false; handle_request = hr} in
(HD.body runtime.HSC.rt_server.HSC.rt_dialog_content sched) { HST.cont=k; request=req; connection=conn; certificate=None }
-let handle_delete sched runtime _method hr (uri, headers) conn k =
- #<If>Logger.debug "handle_delete: uri=%s" uri#<End>;
+let handle_optional handler_name sched runtime _method hr (uri, headers) conn k =
+ #<If>Logger.debug "%s: uri=%s" handler_name uri#<End>;
HSCm.check_host headers;
let req = { HST.request_scheduler=sched;
HST.request_line = { HST._method=_method; request_uri=uri; http_version=HSC.http_version_number };
@@ -541,6 +541,7 @@ type options =
port : int;
block_size : int;
allowed_hosts : string list;
+ allmethods: bool;
dos_prevention : bool;
on_server_run : options -> Scheduler.t -> unit;
on_server_close : Scheduler.t -> unit;
@@ -550,7 +551,7 @@ type options =
-> Scheduler.connection_info -> (HST.response -> unit) -> unit;
put : Scheduler.t -> HSC.runtime -> HSCp.msg -> HST.handle_request -> HST.put
-> Scheduler.connection_info -> (HST.response -> unit) -> unit;
- delete : Scheduler.t -> HSC.runtime -> HSCp.msg -> HST.handle_request -> HST.delete
+ optional : string -> Scheduler.t -> HSC.runtime -> HSCp.msg -> HST.handle_request -> HST.optional
-> Scheduler.connection_info -> (HST.response -> unit) -> unit;
pre_headers : HST.handle_request -> HSCp.msg -> HST.header list -> (HST.handle_request * HST.header list);
post_headers : HST.handle_request -> HSCp.msg -> HST.header list -> HST.header list
@@ -634,12 +635,13 @@ let default_options =
block_size = 4096; (* TODO: implement separate callbac blocksize *)
allowed_hosts = [];
dos_prevention = true;
+ allmethods = false;
on_server_run = (fun _ _ -> ());
on_server_close = (fun _ -> ());
get = handle_get;
post = handle_post;
put = handle_put;
- delete = handle_delete;
+ optional = handle_optional;
pre_headers = pre_headers;
post_headers = post_headers;
callback = Some null_callback;
@@ -809,6 +811,10 @@ let spec_args name =
ServerArg.func ServerArg.unit (fun o () -> { o with backtrace = false }),
"", (sprintf "Disable backtrace printout for server exceptions" (*default_options.backtrace*));
+ p"all-methods",
+ ServerArg.func ServerArg.unit (fun o () -> { o with allmethods = true }),
+ "", (sprintf "Enable all HTTP methods (default: only GET, HEAD and POST)" (*default_options.allmethods*));
+
p"ssl-cert",
ServerArg.func ServerArg.string (fun o s -> { o with ssl_cert = s }),
"<file>", (sprintf "Location of your SSL certificate (requires ssl-key) (default:'%s')" default_options.ssl_cert);
@@ -905,11 +911,12 @@ let make (name:string) (opt:options) (sched:Scheduler.t) : t =
let diff = lc.Unix.tm_hour - gm.Unix.tm_hour in
let sign = if diff > 0 then "+" else if diff < 0 then "-" else "" in
HST.time_diff := sprintf "%s%02d00" sign diff;
+ Printf.eprintf "all_methods: %b\n%!" opt.allmethods;
let runtime = {
HSC.rt_get = opt.get;
rt_post = opt.post;
rt_put = opt.put;
- rt_delete = opt.delete;
+ rt_optional = opt.optional;
rt_core =
{ HSC.rt_pre_headers = opt.pre_headers;
rt_post_headers = opt.post_headers;
@@ -932,6 +939,7 @@ let make (name:string) (opt:options) (sched:Scheduler.t) : t =
rt_on_close = opt.on_server_close;
rt_favicon_ico = opt.favicon_ico;
rt_favicon_gif = opt.favicon_gif;
+ rt_all_methods = opt.allmethods;
};
rt_proto =
{ HSC.rt_name = opt.name;
View
44 libnet/httpServerCore.proto
@@ -70,6 +70,7 @@
rt_on_close : Scheduler.t -> unit;
rt_favicon_ico : body_value;
rt_favicon_gif : body_value;
+ rt_all_methods : bool;
}
-type payload = string * (header list)
@@ -91,8 +92,8 @@
-> Scheduler.connection_info -> (response -> unit) -> unit;
rt_put : Scheduler.t -> runtime -> msg -> handle_request -> put
-> Scheduler.connection_info -> (response -> unit) -> unit;
- rt_delete : Scheduler.t -> runtime -> msg -> handle_request -> delete
- -> Scheduler.connection_info -> (response -> unit) -> unit;
+ rt_optional : string -> Scheduler.t -> runtime -> msg -> handle_request -> optional
+ -> Scheduler.connection_info -> (response -> unit) -> unit;
rt_tmp : rt_tmp;
rt_server : rt_server;
rt_proto : rt_proto;
@@ -314,18 +315,14 @@ wait_for_request(hr, timeout):
wait_for_headers0(uri, {{ [] }}, hr, {{ Get (uri,v) }}, {{ -1 }}, 0, timeout)
| Post (uri, v) ->
wait_for_headers0(uri, {{ [] }}, hr, {{ Post (uri,v) }}, {{ -1 }}, 0, timeout)
- | Put (uri, v) ->
- wait_for_headers0(uri, {{ [] }}, hr, {{ Put (uri,v) }}, {{ -1 }}, 0, timeout)
- | Del (uri, v) ->
- wait_for_headers0(uri, {{ [] }}, hr, {{ Del (uri,v) }}, {{ -1 }}, 0, timeout)
% If we get a secure connection on a non-secure server we get lots of rubbish here
% so we only respond with a bad request if it is actually a request.
- %| Put (uri, v) -> bad_request({{ Put (uri,v) }})
- %| Del (uri, v) -> bad_request({{ Del (uri,v) }})
- | Trace (uri, v) -> bad_request({{ Trace (uri,v) }})
- | Conn (uri, v) -> bad_request({{ Conn (uri,v) }})
- | Opts (uri, v) -> bad_request({{ Opts (uri,v) }})
- | Ptch (uri, v) -> bad_request({{ Ptch (uri,v) }})
+ | Put (uri, v) -> optional_method(uri, hr, {{ Put (uri,v) }}, timeout)
+ | Del (uri, v) -> optional_method(uri, hr, {{ Del (uri,v) }}, timeout)
+ | Trace (uri, v) -> optional_method(uri, hr, {{ Trace (uri,v) }}, timeout)
+ | Conn (uri, v) -> optional_method(uri, hr, {{ Conn (uri,v) }}, timeout)
+ | Opts (uri, v) -> optional_method(uri, hr, {{ Opts (uri,v) }}, timeout)
+ | Ptch (uri, v) -> optional_method(uri, hr, {{ Ptch (uri,v) }}, timeout)
| msg ->
let amount_of_rubbish = {{ String.length (string_of_msg msg) }}
{{ Logger.error "HttpServerCore.wait_for_request: bad request (%d bytes of rubbish)" amount_of_rubbish }}
@@ -340,6 +337,11 @@ wait_for_request(hr, timeout):
{{ if _runtime.rt_proto.rt_backtrace then (Some (Printexc.get_backtrace())) else None }})
after timeout -> close_connection_req ({{"wait_for_request"}}, {{ None }})
+optional_method(uri, hr, request_type, timeout):
+ if {{ _runtime.rt_server.rt_all_methods }}
+ then wait_for_headers0(uri, {{ [] }}, hr, request_type, {{ -1 }}, 0, timeout)
+ else bad_request(request_type)
+
wait_for_headers0(uri, headers, hr, request_type, contlen, hdrcnt, timeout):
debug 10 {{ Logger.debug "wait_for_headers: %s\n%!" (string_of_msg request_type) }}
let tm = {{ Time.now () }}
@@ -366,7 +368,11 @@ wait_for_headers(uri, headers, hr, request_type, contlen, hdrcnt, timeout):
| Get _ -> handle_get(request_type, uri, headers, hr)
| Post _ -> handle_post(request_type, contlen, uri, headers, hr, timeout)
| Put _ -> handle_put(request_type, contlen, uri, headers, hr, timeout)
- | Del _ -> handle_delete(request_type, uri, headers, hr)
+ | Del _ -> handle_optional(request_type, uri, headers, hr, {{"DELETE"}}, {{"handle_delete"}})
+ | Trace _ -> handle_optional(request_type, uri, headers, hr, {{"TRACE"}}, {{"handle_trace"}})
+ | Conn _ -> handle_optional(request_type, uri, headers, hr, {{"CONNECT"}}, {{"handle_connect"}})
+ | Opts _ -> handle_optional(request_type, uri, headers, hr, {{"OPTIONS"}}, {{"handle_options"}})
+ | Ptch _ -> handle_optional(request_type, uri, headers, hr, {{"PATCH"}}, {{"handle_patch"}})
| rt -> bad_request(rt)
;
end
@@ -440,16 +446,16 @@ handle_put0(request_type, contlen, uri, headers, hr, timeout):
after timeout -> timeouterr ({{"handle_put"}}, {{ None }})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%% Handling DELETE requests %%%%
+%%%% Handling optional requests %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-handle_delete(request_type, uri, headers, hr):
- let _runtime = {{ set_payload _runtime ("DELETE",headers) }}
+handle_optional(request_type, uri, headers, hr, reqname, handlername):
+ let _runtime = {{ set_payload _runtime (reqname,headers) }}
if {{ http_server_callback _runtime _mailbox }}
then
- debug {{ Logger.debug "handle_delete: uri=%s\n%!" uri }}
- let res = !"handle_delete"<< _runtime.rt_delete sched _runtime request_type hr (uri, headers) conn >>
+ debug {{ Logger.debug "%s: uri=%s\n%!" handlername uri }}
+ let res = !"handle_optional"<< _runtime.rt_optional handlername sched _runtime request_type hr (uri, headers) conn >>
send_res(res, hr, request_type, headers, NoResource)
- else callback_abort({{"handle_delete"}}, {{None}})
+ else callback_abort(handlername, {{None}})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Handling POST requests %%%%
View
2 libnet/httpServerTypes.ml
@@ -36,7 +36,7 @@ type post =
| Multipart of multipart_post_request
| Simple of uri * header list * Rcontent.content
type put = uri * header list * Rcontent.content
-type delete = uri * header list
+type optional = uri * header list
type request_line = { _method : msg ; request_uri : string ; http_version : string }
View
2 libnet/httpServerTypes.mli
@@ -36,7 +36,7 @@ type post =
| Multipart of multipart_post_request
| Simple of uri * header list * Rcontent.content
type put = uri * header list * Rcontent.content
-type delete = uri * header list
+type optional = uri * header list
type request_line = { _method : msg ; request_uri : string ; http_version : string }

0 comments on commit d0d0150

Please sign in to comment.
Something went wrong with that request. Please try again.