/
http.R
77 lines (63 loc) · 2.06 KB
/
http.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
imports "http" from "Rhttp";
# title: R# web http server
# author: xieguigang
# description: a commandline R# script for running a http web server.
[@info "the http port for listen, 80 port number is used by default."]
const httpPort as integer = ?"--listen" || 80;
[@info "A directory path that contains the R script for running in this R# web server."]
[@type "directory"]
const webContext as string = ?"--wwwroot" || `${dirname(@script)}/../web.R`;
#' Route url as local R script file
#'
#' @param url the url object that parsed from the
#' http request.
#'
const router = function(url) {
`${webContext}/${ trim(url$path, ".") }.R`;
}
#' Handle http GET request
#'
const handleHttpGet = function(req, response) {
const R as string = router(getUrl(req));
print("request from the browser client:");
str(getUrl(req));
print("view the request data headers:");
str(getHeaders(req));
print("this is the unparsed raw text of the http header message:");
print(getHttpRaw(req));
if (file.exists(R)) {
writeLines(source(R), con = response);
} else {
response
|> httpError(404, `the required Rscript file is not found on filesystem location: '${ normalizePath(R) }'!`)
;
}
}
#' Handle http POST request
#'
const handleHttpPost = function(req, response) {
const R as string = router(getUrl(req));
str(getUrl(req));
str(getHeaders(req));
print(getHttpRaw(req));
if (file.exists(R)) {
writeLines(source(R), con = response);
} else {
response
|> httpError(404, `the required Rscript file is not found on filesystem location: '${ normalizePath(R) }'!`)
;
}
}
cat("\n\n");
http::http_socket()
|> headers(
"X-Powered-By" = "R# web server",
"Author" = "xieguigang <xie.guigang@gcmodeller.org>",
"Github" = "https://github.com/rsharp-lang/Rserver",
"Organization" = "R# language <https://github.com/rsharp-lang/>"
)
|> httpMethod("GET", handleHttpGet)
|> httpMethod("POST", handleHttpPost)
|> httpMethod("PUT", [req, response] => writeLines("HTTP PUT test success!", con = response))
|> listen(port = httpPort)
;