Debian Codegraph @

R/middleware-shiny.R

raw
1 #' @include globals.R
2 NULL
3
4 reactLogHandler <- function(req) {
5 if (! rLog$isLogging()) {
6 if (
7 identical(req$PATH_INFO, "/reactlog/mark") ||
8 identical(req$PATH_INFO, "/reactlog")
9 ) {
10 # is not logging, but is a reactlog path...
11
12 return(
13 httpResponse(
14 # Not Implemented
15 # - The server either does not recognize the request method, or it lacks the ability to fulfil the request.
16 status = 501,
17 content_type = "text/plain; charset=utf-8",
18 content = "To enable reactlog, set the following option before running the application: \n\noptions(shiny.reactlog = TRUE)"
19 )
20 )
21
22 } else {
23 # continue on like normal
24 return(NULL)
25 }
26
27 }
28
29 if (identical(req$PATH_INFO, "/reactlog/mark")) {
30 sessionToken <- parseQueryString(req$QUERY_STRING)$s
31 shinysession <- appsByToken$get(sessionToken)
32
33 # log time
34 withReactiveDomain(shinysession, {
35 rLog$userMark(getDefaultReactiveDomain())
36 })
37
38 return(httpResponse(
39 status = 200,
40 content = "marked",
41 content_type = "text/plain"
42 ))
43
44 } else if (identical(req$PATH_INFO, "/reactlog")){
45
46 sessionToken <- parseQueryString(req$QUERY_STRING)$s
47
48 # `renderReactLog` will check/throw if reactlog doesn't exist
49 reactlogFile <- renderReactlog(sessionToken)
50
51 return(httpResponse(
52 status = 200,
53 content = list(
54 file = reactlogFile,
55 owned = TRUE
56 )
57 ))
58
59 } else {
60 # continue on like normal
61 return(NULL)
62 }
63 }
64
65 sessionHandler <- function(req) {
66 path <- req$PATH_INFO
67 if (is.null(path))
68 return(NULL)
69
70 matches <- regmatches(path, regexec('^(/session/([0-9a-f]+))(/.*)$', path))
71 if (length(matches[[1]]) == 0)
72 return(NULL)
73
74 session <- matches[[1]][3]
75 subpath <- matches[[1]][4]
76
77 shinysession <- appsByToken$get(session)
78 if (is.null(shinysession))
79 return(NULL)
80
81 subreq <- as.environment(as.list(req, all.names=TRUE))
82 subreq$PATH_INFO <- subpath
83 subreq$SCRIPT_NAME <- paste(subreq$SCRIPT_NAME, matches[[1]][2], sep='')
84
85 withReactiveDomain(shinysession, {
86 shinysession$handleRequest(subreq)
87 })
88 }