Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support callv and proxies for lsp.js #10172

Merged
merged 10 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion js/language_server/Main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ let _ =
RPC_server.io_ref := (module Io);
Logs.set_level (Some Logs.Debug);
Logs.set_reporter { Logs.report = Semgrep_js_shared.console_report };
Http_helpers.client_ref := Some (module Cohttp_lwt_jsoo.Client);
Session.scan_config_parser_ref := scan_config_parser;

let server =
Expand Down
2 changes: 0 additions & 2 deletions js/language_server/dune
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
lwt_platform.js

networking.http_helpers
cohttp-lwt-jsoo
)
(modes js)
(js_of_ocaml
(javascript_files
;; Custom XMLHttpRequest implementation that doesn't preset the User-Agent header
ajbt200128 marked this conversation as resolved.
Show resolved Hide resolved
XMLHttpRequest.js
semgrep.js
))
(preprocess
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,17 @@ XMLHttpRequest = function () {
headers["Content-Length"] = 0;
}

// get if HTTP_PROXY or HTTPS_PROXY is set
var proxy = process.env[ssl ? "HTTPS_PROXY" : "HTTP_PROXY"];
ajbt200128 marked this conversation as resolved.
Show resolved Hide resolved
if (proxy) {
// set host to proxy
const proxyUrl = Url.parse(proxy);
host = proxyUrl.hostname;
port = proxyUrl.port;
// set path to full URL
uri = url.href;

}
var options = {
host: host,
port: port,
Expand Down
1 change: 1 addition & 0 deletions js/node_shared/dune
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
(js_of_ocaml
(javascript_files
unix.js
XMLHttpRequest.js ; for making HTTP requests
))
(preprocess
(pps js_of_ocaml-ppx lwt_ppx)))
19 changes: 19 additions & 0 deletions js/shared/Semgrep_js_shared.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ external set_parser_wasm_module : 'any -> unit = "set_parser_wasm_module"
(* Helpers *)
(*****************************************************************************)

module Patched_cohttp_lwt_jsoo : Cohttp_lwt.S.Client = struct
include Cohttp_lwt_jsoo.Client

(* Callv isn't currently supported on the JS side so we have to provide it ourselves :( *)
let callv ?ctx (uri : Uri.t) stream =
(* Differs from request uri if HTTP(S)_PROXY is set. But we handle this in
the node_shared XHR*)
ignore uri;
Lwt.return
(Lwt_stream.map_s
(fun ((req, body) : Cohttp.Request.t * Cohttp_lwt.Body.t) ->
call ?ctx ~headers:req.headers ~body ~chunked:false req.meth
Cohttp.Request.(uri req))
stream)
end

let wrap_with_js_error ?(hook = None) f =
try f () with
| e ->
Expand All @@ -49,6 +65,9 @@ let init_jsoo yaml_wasm_module =
Common.jsoo := true;
Tree_sitter_run.Util_file.jsoo := true;
Metrics_.g.payload.environment.isTranspiledJS <- true;
(* NOTE: HTTP stuff won't work on node unless node_shared is included *)
Http_helpers.client_ref :=
Some (module Patched_cohttp_lwt_jsoo : Cohttp_lwt.S.Client);
(* Using semgrep.parsing_languages makes the JS goes
from 16MB to 7MB (in release mode) and from 110MB to 50MB (in dev mode)
TODO: we should add language parsers dynamically, loading language "bundles"
Expand Down
1 change: 1 addition & 0 deletions js/shared/dune
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
semgrep.data
semgrep.language_server
lwt_platform.js
cohttp-lwt-jsoo
tracing.js
)
(js_of_ocaml
Expand Down
10 changes: 10 additions & 0 deletions libs/networking/http_helpers/http_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ struct
(* Respect the proxy!*)
(* Cohttp doesn't https://github.com/mirage/ocaml-cohttp/issues/459 *)
(* https://github.com/0install/0install/blob/6c0f5c51bc099370a367102e48723a42cd352b3b/ocaml/zeroinstall/http.cohttp.ml#L232-L256 *)

(* What do we have to do to support a proxy? *)
(* On Native OCaml (not js), the certificate MUST be in a path listed in
https://github.com/mirage/ca-certs/blob/fa4ff942f1ac980e2502a0783ef10ade5ba497f2/lib/ca_certs.ml#L34-L50 *)
(* HTTP(s)_PROXY MUST have a scheme, http:// or https://, and must have a port
if it isn't on port 80/443 *)
(* So HTTP_PROXY=http://localhost:8080 or
HTTPS_PROXY=https://localhost:8080 *)
(* And note that HTTP_PROXY is only used for HTTP requests! We almost
exclusively use HTTPS urls so make sure both are set*)
let get_proxy uri =
let proxy_uri_env =
match Uri.scheme uri with
Expand Down