-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttpHelp.R
156 lines (133 loc) · 3.88 KB
/
httpHelp.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
imports "http" from "Rstudio";
imports "help" from "Rstudio";
# 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" || "./";
help::http_load();
# /search?q=keyword
#' Route url as local R script file
#'
#' @param url the url object that parsed from the
#' http request.
#'
const router = function(url, headers) {
const relpath as string = trim(url$path, ".");
const httpHelp = {
"/search": any -> help::search(url$query$q),
"/browse": any -> help::browse(url$query$pkg),
"/vignettes": any -> help::vignettes(url$query$q)
};
if ([relpath == ""] || [relpath == "/"]) {
list(
file = help::index(),
is_script = FALSE,
is_html = TRUE
);
} else {
if (!is.null(httpHelp[[relpath]])) {
const api = httpHelp[[relpath]];
list(
file = api(url),
is_script = FALSE,
is_html = TRUE
);
} else {
localfile_router(relpath, url, headers);
}
}
}
const localfile_router = function(relpath, url, headers) {
const refer = http::parseUrl(headers$Referer);
str(refer);
if ([file.ext(relpath) == "html"] && [refer$path == "vignettes"]) {
list(
file = help::vignettes(relpath, context = refer$query$q),
is_script = FALSE,
is_html = TRUE
);
} else {
const isMap_temp as boolean = startsWith(url$path, "@temp");
const tempfile as string = {
if(isMap_temp) {
gsub(relpath, "@temp", getOption("system_tempdir"))
} else {
`${webContext}/${relpath}`;
}
}
print("non-script file:");
print(tempfile);
list(
file = normalizePath(tempfile),
is_script = FALSE,
is_html = FALSE
);
}
}
#' Handle http GET request
#'
const handleHttpGet = function(req, response) {
const local = router(getUrl(req), getHeaders(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 ([local$is_script] && file.exists(local$file)) {
print(`run script: '${local$file}'!`);
writeLines(source(local$file), con = response);
} else {
if (!local$is_script) {
if (local$is_html) {
writeLines(local$file, con = response);
} else {
if (file.ext(local$file) in ["html","htm","txt"]) {
print("Response a html text file!");
writeLines(readText(local$file), con = response);
} else {
print(`Push file downloads: '${local$file}'!`);
response
|> pushDownload(local$file)
;
}
}
} else {
response
|> httpError(404, `the required Rscript file is not found on filesystem location: '${ normalizePath(local$file) }'!`)
;
}
}
}
#' 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, accessAny = TRUE)
|> httpMethod("POST", handleHttpPost, accessAny = TRUE)
|> httpMethod("PUT", [req, response] => writeLines("HTTP PUT test success!", con = response), accessAny = TRUE)
|> listen(port = httpPort)
;