1
#########################################################################/**
2
# @set "class=HttpDaemon"
3
# @RdocMethod processRsp
4
#
5
# @title "Processes an RSP page"
6
#
7
# \description{
8
#  @get "title".
9
# }
10
#
11
# @synopsis
12
#
13
# \arguments{
14
#   \item{pathname}{The RSP file to be processed.}
15
#   \item{version}{The version of the RSP processor to use.}
16
#   \item{...}{Not used.}
17
# }
18
#
19
# \value{
20
#  Returns nothing.
21
# }
22
#
23
# \section{Settings}{
24
#   The \pkg{R.rsp} package implements different RSP engines.
25
#   It is possible to specify which version the Tcl HTTP daemon
26
#   should use via the option \code{R.rsp/HttpDaemon/RspVersion}.
27
#   The default is now to use the new RSP engine, which corresponds
28
#   \code{options("R.rsp/HttpDaemon/RspVersion"="1.0.0")}.
29
#   The old legacy RSP engine \code{"0.1.0"} is defunct.
30
# }
31
#
32
# @author
33
#
34
# \seealso{
35
#   @seeclass
36
# }
37
#
38
# @keyword IO
39
#*/#########################################################################
40
setMethodS3("processRsp", "HttpDaemon", function(static=getStaticInstance(HttpDaemon), pathname=tcltk::tclvalue("mypath"), version=getOption("R.rsp/HttpDaemon/RspVersion", "1.0.0"), ...) {
41
  # If processRsp() was called from Tcl, then it is called without
42
  # arguments, which is why we need this rather ad hoc solution to
43
  # default 'static' to getStaticInstance().
44 0
  daemon <- static
45

46

47
  # Use a "global" tryCatch() to catch and respond to RSP processing errors
48 0
  tryCatch({
49

50
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51
  # Validate arguments
52
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53
  # Argument 'pathname':
54 0
  pathname <- as.character(pathname)
55

56
  # Validate pathname
57 0
  pathname <- Arguments$getReadablePathname(pathname)
58

59
  # Argment 'version':
60 0
  if (!is.element(version, c("0.1.0", "1.0.0"))) {
61 0
    throw("Unknown HttpDaemon RSP version: ", version)
62
  }
63

64 0
  if (version == "0.1.0") {
65 0
    .Defunct(msg = "RSP HTTP daemon v0.1.0 is defunct, because it relies on an old legacy RSP engine, which has been removed. Use v1.0.0 instead, by removing options 'R.rsp/HttpDaemon/RspVersion' or setting it to '1.0.0'.")
66
  }
67

68 0
  debug <- isTRUE(daemon$.debug)
69

70 0
  if (debug) {
71 0
    mcat("DEBUG: RSP version: ", version, "\n", sep="")
72
  }
73

74
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75
  # Setup
76
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77 0
  path <- getParent(pathname)
78 0
  filename <- basename(pathname)
79

80
  # Record the current directory
81 0
  opwd <- getwd()
82 0
  on.exit(setwd(opwd))
83

84
  # Set the current working directory of the HTTP daemon
85 0
  daemon$pwd <- opwd
86 0
  setwd(path)
87

88
  # Get the HTTP request information
89 0
  request <- getHttpRequest(daemon)
90

91 0
  if (debug) {
92 0
    mcat("DEBUG: RSP file: ", pathname, "\n", sep="")
93 0
    mcat("DEBUG: Working directory: ", path, "\n", sep="")
94
  }
95

96
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97
  # Process RSP file
98
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99 0
  if (version == "1.0.0") {
100 0
    response <- HttpDaemonRspResponse(httpDaemon=daemon)
101 0
    page <- RspPage(pathname)
102 0
    pathnameR <- rfile(file=filename, workdir=opwd, args=list(page=page, request=request, response=response))
103 0
    s <- readLines(pathnameR, warn=FALSE)
104 0
    s <- paste(s, collapse="\n")
105 0
    if (nchar(s) > 0L) writeResponse(daemon, s)
106
  }
107

108 0
  }, error = function(ex) {
109 0
    mcat("ERROR:")
110 0
    mprint(ex)
111 0
    writeResponse(daemon, as.character(ex))
112 0
    if (!is.null(ex$code)) {
113 0
      code <- paste(ex$code, collapse="\n")
114 0
      mcat(code, "\n")
115 0
      mprint(ex)
116 0
      writeResponse(daemon, code)
117
    }
118 0
  }) # tryCatch()
119
}, static=TRUE, protected=TRUE)

Read our documentation on viewing source code .

Loading