Skip to content

Commit dee989f

Browse files
authored
xapi-stdext-std: remove most functions, optimize escaping functions (#6795)
The Listext module has a lot of baggage that can be replace with Stdlib, Astring. And what cannot be replaced, it can be made better, especially the escaping. There are quite a few changes, so it's better to review commit-by-commit. I need to do some testing to make sure all changes here are safe and undraft this
2 parents 01a4cb2 + c9214fa commit dee989f

58 files changed

Lines changed: 243 additions & 576 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc/content/xapi/cli/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ So each function receives a printer for sending text output to the xe client, an
156156
let mac = List.assoc_default "mac" params "" in
157157
let network = Client.Network.get_by_uuid rpc session_id network in
158158
let pifs = List.assoc "pif-uuids" params in
159-
let uuids = String.split ',' pifs in
159+
let uuids = String.split_on_char ',' pifs in
160160
let pifs = List.map (fun uuid -> Client.PIF.get_by_uuid rpc session_id uuid) uuids in
161161
let mode = Record_util.bond_mode_of_string (List.assoc_default "mode" params "") in
162162
let properties = read_map_params "properties" params in

ocaml/database/parse_db_conf.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*)
1414
(* !!! This needs to be moved out of xapi and into the database directory; probably being merged with db_connections !!! *)
1515

16-
open Xapi_stdext_std.Xstringext
1716
open Xapi_stdext_unix
1817

1918
module D = Debug.Make (struct let name = "parse_db_conf" end)
@@ -110,7 +109,7 @@ let parse_db_conf s =
110109
let conf = Unixext.string_of_file s in
111110
let lines : string list ref = ref [] in
112111
let consume_line () = lines := List.tl !lines in
113-
lines := String.split '\n' conf ;
112+
lines := String.split_on_char '\n' conf ;
114113
List.iter (fun line -> debug "%s" line) !lines ;
115114
let read_block () =
116115
let path_line = List.hd !lines in
@@ -120,7 +119,7 @@ let parse_db_conf s =
120119
while !lines <> [] && List.hd !lines <> "" do
121120
let line = List.hd !lines in
122121
key_values :=
123-
( match String.split ':' line with
122+
( match String.split_on_char ':' line with
124123
| k :: vs ->
125124
( String.lowercase_ascii k
126125
, String.lowercase_ascii (String.concat ":" vs)

ocaml/database/redo_log.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* GNU Lesser General Public License for more details.
1313
*)
1414
open Xapi_stdext_pervasives.Pervasiveext
15-
open Xapi_stdext_std.Xstringext
1615
open Xapi_stdext_unix
1716

1817
let with_lock = Xapi_stdext_threads.Threadext.Mutex.execute

ocaml/doc/dune

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(executable
22
(modes exe)
33
(name jsapi)
4-
(libraries
4+
(libraries
55
mustache
66
rpclib.core
77
rpclib.json
@@ -10,7 +10,6 @@
1010
xapi-consts
1111
xapi-datamodel
1212
xapi-stdext-pervasives
13-
xapi-stdext-std
1413
xapi-stdext-unix
1514
)
1615
(preprocess (pps ppx_deriving_rpc))

ocaml/doc/jsapi.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* GNU Lesser General Public License for more details.
1313
*)
1414

15-
open Xapi_stdext_std.Xstringext
1615
open Xapi_stdext_pervasives.Pervasiveext
1716
module Unixext = Xapi_stdext_unix.Unixext
1817
open Datamodel_types

ocaml/idl/markdown_backend.ml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,41 @@ let compare_case_ins x y =
4343
compare (String.lowercase_ascii x) (String.lowercase_ascii y)
4444

4545
let escape s =
46-
let esc_char = function
46+
let replace = function
4747
| '\\' ->
48-
"&#92;"
48+
Some "&#92;"
4949
| '*' ->
50-
"&#42;"
50+
Some "&#42;"
5151
| '_' ->
52-
"&#95;"
52+
Some "&#95;"
5353
| '{' ->
54-
"&#123;"
54+
Some "&#123;"
5555
| '}' ->
56-
"&#125;"
56+
Some "&#125;"
5757
| '[' ->
58-
"&#91;"
58+
Some "&#91;"
5959
| ']' ->
60-
"&#93;"
60+
Some "&#93;"
6161
| '(' ->
62-
"&#40;"
62+
Some "&#40;"
6363
| ')' ->
64-
"&#41;"
64+
Some "&#41;"
6565
| '>' ->
66-
"&gt;"
66+
Some "&gt;"
6767
| '<' ->
68-
"&lt;"
68+
Some "&lt;"
6969
| '#' ->
70-
"&#35;"
70+
Some "&#35;"
7171
| '+' ->
72-
"&#43;"
72+
Some "&#43;"
7373
| '-' ->
74-
"&#45;"
74+
Some "&#45;"
7575
| '!' ->
76-
"&#33;"
77-
| c ->
78-
String.make 1 c
76+
Some "&#33;"
77+
| _ ->
78+
None
7979
in
80-
String.to_seq s |> Seq.map esc_char |> List.of_seq |> String.concat ""
80+
Xapi_stdext_std.Xstringext.String.replaced ~replace s
8181

8282
let rec of_ty_verbatim = function
8383
| SecretString | String ->

ocaml/idl/ocaml_backend/gen_rbac.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ let role_uuid name = Option.get (hash2uuid name)
8080
let permission_description = "A basic permission"
8181

8282
let permission_name wire_name =
83-
let open Xapi_stdext_std in
8483
let s1 = replace_char (Printf.sprintf "permission_%s" wire_name) '.' '_' in
8584
let s2 = replace_char s1 '/' '_' in
86-
let s3 = Xstringext.String.replace "*" "WILDCHAR" s2 in
87-
Xstringext.String.replace ":" "_" s3
85+
let s3 = Xapi_stdext_std.Xstringext.String.replace '*' ~by:"WILDCHAR" s2 in
86+
replace_char s3 ':' '_'
8887

8988
let permission_index = ref 0
9089

ocaml/libs/http-lib/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
xapi-log.backtrace
6464
xapi-log
6565
xapi-stdext-pervasives
66+
xapi-stdext-std
6667
xapi-stdext-threads
6768
xapi-stdext-unix))
6869

ocaml/libs/http-lib/http_svr.ml

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -340,28 +340,22 @@ module Server = struct
340340
x.handlers []
341341
end
342342

343-
let escape str =
344-
(* from xapi-stdext-std xstringext *)
345-
let escaped ~rules string =
346-
let aux h t =
347-
( if List.mem_assoc h rules then
348-
List.assoc h rules
349-
else
350-
Astring.String.of_char h
343+
let escape_html str =
344+
Xapi_stdext_std.Xstringext.String.replaced
345+
~replace:(function
346+
| '<' ->
347+
Some "&lt;"
348+
| '>' ->
349+
Some "&gt;"
350+
| '\'' ->
351+
Some "&apos;"
352+
| '"' ->
353+
Some "&quot;"
354+
| '&' ->
355+
Some "&amp;"
356+
| _ ->
357+
None
351358
)
352-
:: t
353-
in
354-
String.concat "" (Astring.String.fold_right aux string [])
355-
in
356-
escaped
357-
~rules:
358-
[
359-
('<', "&lt;")
360-
; ('>', "&gt;")
361-
; ('\'', "&apos;")
362-
; ('"', "&quot;")
363-
; ('&', "&amp;")
364-
]
365359
str
366360

367361
exception Generic_error of string
@@ -518,7 +512,7 @@ let read_request ?proxy_seen ~read_timeout ~total_timeout ~max_length fd =
518512
)
519513
| exc ->
520514
response_internal_error exc fd
521-
~extra:(escape (Printexc.to_string exc))
515+
~extra:(escape_html (Printexc.to_string exc))
522516
) ;
523517
(None, None)
524518

@@ -567,7 +561,7 @@ let handle_one (x : 'a Server.t) ss context req =
567561
)
568562
| exc ->
569563
response_internal_error ~req exc ss
570-
~extra:(escape (Printexc.to_string exc))
564+
~extra:(escape_html (Printexc.to_string exc))
571565
) ;
572566
!finished
573567

ocaml/libs/http-lib/http_svr.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ end
4949

5050
exception Generic_error of string
5151

52-
val escape : string -> string
53-
(** [escape str] escapes HTML/XML special characters in [str] for safe inclusion in HTML/XML content. *)
52+
val escape_html : string -> string
53+
(** [escape_html str] escapes HTML/XML special characters in [str] for safe inclusion in HTML/XML content. *)
5454

5555
type socket
5656

0 commit comments

Comments
 (0)