Skip to content

Commit d7a029b

Browse files
authored
Updates to HTTP request paths in preparation for removing prefix matching behaviour. (#6732)
We're looking to remove the prefix matching that our homebrew HTTP server does with routes, so I've updated a couple things in the code to remove any reliance on this behaviour. In particular: - The Python RPC library makes requests to the /RPC2 endpoint, which used to get redirected by the prefix matching, so I've added that as a valid endpoint. - nbd was making requests to "/var/xapi/xapi" in error, as this is the socket it was connecting to. - "uri" in the HTTP server actually referred to the path, not the entire uri - changed this.
2 parents b1ae081 + 78ebb86 commit d7a029b

31 files changed

+104
-80
lines changed

ocaml/idl/datamodel.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11005,6 +11005,16 @@ let http_actions =
1100511005
, []
1100611006
)
1100711007
)
11008+
(* For XC < 8460 compatibility, remove when out of support *)
11009+
; ( "get_vm_rrds"
11010+
, ( Get
11011+
, "/vm_rrds"
11012+
, true
11013+
, [String_query_arg "uuid"; Bool_query_arg "json"]
11014+
, _R_READ_ONLY
11015+
, []
11016+
)
11017+
)
1100811018
; ( Constants.get_host_rrd
1100911019
, ( Get
1101011020
, Constants.get_host_rrd_uri
@@ -11014,6 +11024,10 @@ let http_actions =
1101411024
, []
1101511025
)
1101611026
)
11027+
(* For XC < 8460 compatibility, remove when out of support *)
11028+
; ( "get_host_rrds"
11029+
, (Get, "/host_rrds", true, [Bool_query_arg "json"], _R_READ_ONLY, [])
11030+
)
1101711031
; ( Constants.get_sr_rrd
1101811032
, ( Get
1101911033
, Constants.get_sr_rrd_uri
@@ -11081,6 +11095,7 @@ let http_actions =
1108111095
)
1108211096
; (* XMLRPC callback *)
1108311097
("post_root", (Post, "/", false, [], _R_READ_ONLY, []))
11098+
; ("post_RPC2", (Post, "/RPC2", false, [], _R_READ_ONLY, []))
1108411099
; (* JSON callback *)
1108511100
("post_json", (Post, Constants.json_uri, false, [], _R_READ_ONLY, []))
1108611101
; ("post_root_options", (Options, "/", false, [], _R_READ_ONLY, []))
@@ -11133,6 +11148,7 @@ let http_actions =
1113311148
let public_http_actions_with_no_rbac_check =
1113411149
[
1113511150
"post_root"
11151+
; "post_RPC2"
1113611152
; (* XMLRPC (API) calls -> checks RBAC internally *)
1113711153
"post_cli"
1113811154
; (* CLI commands -> calls XMLRPC *)

ocaml/libs/http-lib/http.ml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ end
505505
module Request = struct
506506
type t = {
507507
m: method_t
508-
; uri: string
508+
; path: string
509509
; query: (string * string) list
510510
; version: string
511511
; frame: bool
@@ -528,7 +528,7 @@ module Request = struct
528528
let empty =
529529
{
530530
m= Unknown ""
531-
; uri= ""
531+
; path= ""
532532
; query= []
533533
; version= ""
534534
; frame= false
@@ -563,7 +563,7 @@ module Request = struct
563563
; host
564564
; user_agent= Some user_agent
565565
; m= meth
566-
; uri= path
566+
; path
567567
; additional_headers= headers
568568
; body
569569
; accept
@@ -577,10 +577,10 @@ module Request = struct
577577
String.concat "; " (List.map (fun (k, v) -> k ^ "=" ^ v) x)
578578
in
579579
Printf.sprintf
580-
"{ frame = %b; method = %s; uri = %s; query = [ %s ]; content_length = [ \
581-
%s ]; transfer encoding = %s; version = %s; cookie = [ %s ]; task = %s; \
582-
subtask_of = %s; content-type = %s; host = %s; user_agent = %s; }"
583-
x.frame (string_of_method_t x.m) x.uri (kvpairs x.query)
580+
"{ frame = %b; method = %s; path = %s; query = [ %s ]; content_length = \
581+
[ %s ]; transfer encoding = %s; version = %s; cookie = [ %s ]; task = \
582+
%s; subtask_of = %s; content-type = %s; host = %s; user_agent = %s; }"
583+
x.frame (string_of_method_t x.m) x.path (kvpairs x.query)
584584
(Option.fold ~none:"" ~some:Int64.to_string x.content_length)
585585
(Option.value ~default:"" x.transfer_encoding)
586586
x.version "(value filtered)" (* cookies *)
@@ -642,7 +642,7 @@ module Request = struct
642642
[(Hdr.connection ^ ": " ^ if x.close then "close" else "keep-alive")]
643643
in
644644
[
645-
Printf.sprintf "%s %s%s HTTP/%s" (string_of_method_t x.m) x.uri query
645+
Printf.sprintf "%s %s%s HTTP/%s" (string_of_method_t x.m) x.path query
646646
x.version
647647
]
648648
@ cookie
@@ -809,17 +809,17 @@ module Url = struct
809809

810810
type scheme = Http of http | File of file
811811

812-
type data = {uri: string; query_params: (string * string) list}
812+
type data = {path: string; query_params: (string * string) list}
813813

814814
type t = scheme * data
815815

816-
let file_equal a b = String.equal a.path b.path
816+
let file_equal (a : file) (b : file) = String.equal a.path b.path
817817

818818
let query_params_equal (ak, av) (bk, bv) =
819819
String.equal ak bk && String.equal av bv
820820

821821
let data_equal a b =
822-
String.equal a.uri b.uri
822+
String.equal a.path b.path
823823
&& List.equal query_params_equal a.query_params b.query_params
824824

825825
let http_equal a b =
@@ -858,7 +858,7 @@ module Url = struct
858858
in
859859
let data =
860860
{
861-
uri= (match Uri.path_unencoded uri with "" -> "/" | path -> path)
861+
path= (match Uri.path_unencoded uri with "" -> "/" | path -> path)
862862
; query_params= Uri.query uri |> List.map query
863863
}
864864
in
@@ -872,19 +872,19 @@ module Url = struct
872872
(scheme ~ssl:true, data)
873873
| Some "file" ->
874874
let scheme = File {path= Uri.path_unencoded uri} in
875-
(scheme, {data with uri= "/"})
875+
(scheme, {data with path= "/"})
876876
| _ ->
877877
failwith "unsupported URI scheme"
878878
with e ->
879879
fail "%s: can't parse '%s': %s" __FUNCTION__ url (Printexc.to_string e)
880880

881-
let data_to_string {uri; query_params= params} =
881+
let data_to_string {path; query_params= params} =
882882
let kvpairs x =
883883
String.concat "&"
884884
(List.map (fun (k, v) -> urlencode k ^ "=" ^ urlencode v) x)
885885
in
886886
let params = if params = [] then "" else "?" ^ kvpairs params in
887-
uri ^ params
887+
path ^ params
888888

889889
let to_string scheme =
890890
let query (k, v) = (k, [v]) in
@@ -893,7 +893,7 @@ module Url = struct
893893
| File {path}, {query_params= params; _} ->
894894
Uri.make ~scheme:"file" ~path ~query:(List.map query params) ()
895895
|> Uri.to_string
896-
| Http h, {uri; query_params= params} ->
896+
| Http h, {path; query_params= params} ->
897897
let auth =
898898
match h.auth with
899899
| Some (Basic (username, password)) ->
@@ -905,16 +905,16 @@ module Url = struct
905905
in
906906
Uri.make
907907
~scheme:(if h.ssl then "https" else "http")
908-
~host:h.host ?port:h.port ?userinfo:auth ~path:uri
908+
~host:h.host ?port:h.port ?userinfo:auth ~path
909909
~query:(List.map query params) ()
910910
|> Uri.to_string
911911
in
912912
debug "%s: %s" __FUNCTION__ str ;
913913
str
914914

915-
let get_uri (_scheme, data) = data.uri
915+
let get_path (_scheme, data) = data.path
916916

917-
let set_uri (scheme, data) u = (scheme, {data with uri= u})
917+
let set_path (scheme, data) u = (scheme, {data with path= u})
918918

919919
let get_query_params (_scheme, data) = data.query_params
920920

ocaml/libs/http-lib/http.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ end
7272
module Request : sig
7373
type t = {
7474
m: method_t
75-
; uri: string
75+
; path: string
7676
; query: (string * string) list
7777
; version: string
7878
; frame: bool
@@ -254,7 +254,7 @@ module Url : sig
254254

255255
type scheme = Http of http | File of file
256256

257-
type data = {uri: string; query_params: (string * string) list}
257+
type data = {path: string; query_params: (string * string) list}
258258

259259
type t = scheme * data
260260

@@ -264,9 +264,9 @@ module Url : sig
264264

265265
val to_string : t -> string
266266

267-
val get_uri : t -> string
267+
val get_path : t -> string
268268

269-
val set_uri : t -> string -> t
269+
val set_path : t -> string -> t
270270

271271
val get_query_params : t -> (string * string) list
272272

ocaml/libs/http-lib/http_svr.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module Helper = struct
103103
include Tracing.Propagator.Make (struct
104104
include Tracing_propagator.Propagator.Http
105105

106-
let name_span req = req.Http.Request.uri
106+
let name_span req = req.Http.Request.path
107107
end)
108108
end
109109

@@ -310,13 +310,13 @@ module Server = struct
310310

311311
let empty default_context = {handlers= MethodMap.empty; default_context}
312312

313-
let add_handler x ty uri handler =
313+
let add_handler x ty path handler =
314314
let existing =
315315
Option.value (MethodMap.find_opt ty x.handlers) ~default:Radix_tree.empty
316316
in
317317
x.handlers <-
318318
MethodMap.add ty
319-
(Radix_tree.insert uri {(TE.empty ()) with TE.handler} existing)
319+
(Radix_tree.insert path {(TE.empty ()) with TE.handler} existing)
320320
x.handlers
321321

322322
let find_stats x m uri =
@@ -386,7 +386,7 @@ let read_request_exn ~proxy_seen ~read_timeout ~total_timeout ~max_length fd =
386386
(* Request-Line = Method SP Request-URI SP HTTP-Version CRLF *)
387387
let uri_t = Uri.of_string uri in
388388
if uri_t = Uri.empty then raise Http_parse_failure ;
389-
let uri = Uri.path_unencoded uri_t in
389+
let path = Uri.path_unencoded uri_t in
390390
let query = Uri.query uri_t |> kvlist_flatten in
391391
let m = Http.method_t_of_string meth in
392392
let version =
@@ -396,7 +396,7 @@ let read_request_exn ~proxy_seen ~read_timeout ~total_timeout ~max_length fd =
396396
(String.length x - String.length prefix)
397397
in
398398
let close = version = "1.0" in
399-
(true, {req with m; uri; query; version; close})
399+
(true, {req with m; path; query; version; close})
400400
| _ ->
401401
raise Http_parse_failure
402402
else
@@ -525,7 +525,7 @@ let handle_one (x : 'a Server.t) ss context req =
525525
let empty = TE.empty () in
526526
let te =
527527
Option.value ~default:empty
528-
(Radix_tree.longest_prefix req.Request.uri method_map)
528+
(Radix_tree.longest_prefix req.Request.path method_map)
529529
in
530530
let@ _ = Tracing.with_child_trace span ~name:"handler" in
531531
te.TE.handler req ss context ;

ocaml/libs/http-lib/http_test.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ module URL = struct
170170
let open Http.Url in
171171
[
172172
( "file:/var/xapi/storage"
173-
, (File {path= "/var/xapi/storage"}, {uri= "/"; query_params= []})
173+
, (File {path= "/var/xapi/storage"}, {path= "/"; query_params= []})
174174
)
175175
; ( "http://root:foo@localhost"
176176
, ( Http
@@ -180,17 +180,17 @@ module URL = struct
180180
; host= "localhost"
181181
; port= None
182182
}
183-
, {uri= "/"; query_params= []}
183+
, {path= "/"; query_params= []}
184184
)
185185
)
186186
; ( "https://google.com/gmail"
187187
, ( Http {auth= None; ssl= true; host= "google.com"; port= None}
188-
, {uri= "/gmail"; query_params= []}
188+
, {path= "/gmail"; query_params= []}
189189
)
190190
)
191191
; ( "https://xapi.xen.org/services/SM"
192192
, ( Http {auth= None; ssl= true; host= "xapi.xen.org"; port= None}
193-
, {uri= "/services/SM"; query_params= []}
193+
, {path= "/services/SM"; query_params= []}
194194
)
195195
)
196196
; ( "https://root:[email protected]:1234/services/SM"
@@ -201,12 +201,12 @@ module URL = struct
201201
; host= "xapi.xen.org"
202202
; port= Some 1234
203203
}
204-
, {uri= "/services/SM"; query_params= []}
204+
, {path= "/services/SM"; query_params= []}
205205
)
206206
)
207207
; ( "https://xapi.xen.org/services/SM?foo=bar"
208208
, ( Http {auth= None; ssl= true; host= "xapi.xen.org"; port= None}
209-
, {uri= "/services/SM"; query_params= [("foo", "bar")]}
209+
, {path= "/services/SM"; query_params= [("foo", "bar")]}
210210
)
211211
)
212212
]
@@ -221,7 +221,7 @@ module URL = struct
221221
let data = "https://xapi.xen.org/services/SM?foo=bar" in
222222
let expected = "https://xapi.xen.org/services/SM/data?foo=bar" in
223223
let u = Http.Url.of_string data in
224-
let u' = Http.Url.set_uri u (Http.Url.get_uri u ^ "/data") in
224+
let u' = Http.Url.set_path u (Http.Url.get_path u ^ "/data") in
225225
let actual = Http.Url.to_string u' in
226226
Alcotest.(check string) data expected actual
227227

ocaml/libs/http-lib/ws_helpers.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222
(* Websockets helper functions *)
2323

24-
(* A couple of short helper functions for upgrading an HTTP
25-
* connection to a websockets connection
24+
(* A couple of short helper functions for upgrading an HTTP
25+
* connection to a websockets connection
2626
* See for reference:
27-
* http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
27+
* http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
2828
*)
2929

3030
type protocol = Hixie76 | Hybi10
@@ -123,7 +123,7 @@ let hixie_v76_upgrade req s =
123123
try Some (find_header headers "sec-websocket-protocol") with _ -> None
124124
in
125125
let real_uri =
126-
req.Http.Request.uri
126+
req.Http.Request.path
127127
^ "?"
128128
^ String.concat "&"
129129
(List.map

ocaml/nbd/lib/consts.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(** Xapi's local Unix domain socket *)
2-
let xapi_unix_domain_socket_uri = "file:///var/xapi/xapi"
2+
let xapi_unix_domain_socket_uri =
3+
Uri.make ~scheme:"file" ~path:"/var/xapi/xapi" ()
34

45
(** Location of the xensource-inventory file on XenServer *)
56
let xensource_inventory_filename = "/etc/xensource-inventory"

ocaml/nbd/lib/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(library
22
(name consts)
33
(modes best)
4+
(libraries uri)
45
(modules consts)
56
)
67

ocaml/xapi-cli-server/xapi_cli.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ module TraceHelper = struct
125125
include Tracing.Propagator.Make (struct
126126
include Tracing_propagator.Propagator.Http
127127

128-
let name_span req = req.Http.Request.uri
128+
let name_span req = req.Http.Request.path
129129
end)
130130

131131
let inject_span_into_req (span : Tracing.Span.t option) =

ocaml/xapi-guard/test/xapi_guard_test.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ let with_rpc f switch () =
8686
Server_interface.make_server_varstored push_nothing ~cache path vm_uuid
8787
in
8888
(* rpc simulates what varstored would do *)
89-
let uri = Uri.make ~scheme:"file" ~path () |> Uri.to_string in
90-
D.debug "Connecting to %s" uri ;
89+
let uri = Uri.make ~scheme:"file" ~path () in
90+
D.debug "Connecting to %s" (Uri.to_string uri) ;
9191
let rpc = make uri in
9292
Lwt.finalize
9393
(fun () ->

0 commit comments

Comments
 (0)