1
###########################################################################/**
2
# @RdocClass HttpDaemonRspResponse
3
#
4
# @title "The HttpDaemonRspResponse class"
5
#
6
# \description{
7
#  @classhierarchy
8
#
9
#  An instance of class HttpDaemonRspResponse, which extends the
10
#  @see "RspResponse" class, is a buffer for output (response) sent to an
11
#  @see "HttpDaemon".  It provides a method \code{writeResponse()} for writing
12
#  output and a method \code{flush()} for flush the written output to
13
#  the HTTP daemon.
14
# }
15
#
16
# @synopsis
17
#
18
# \arguments{
19
#   \item{httpDaemon}{An @see "HttpDaemon" object.}
20
#   \item{...}{Not used.}
21
# }
22
#
23
# \section{Fields and Methods}{
24
#  @allmethods
25
# }
26
#
27
# \details{
28
#  The purpose of this method is to provide partial writing of HTTP response
29
#  such that, for instance, a web browser can display parts of an HTML page
30
#  while the rest is generated.  Note that this is only supported by the
31
#  HTTP v1.1 protocol.
32
#
33
#  \emph{Note:
34
#   The minimalistic HTTP daemon (written in Tcl) used internally
35
#   currently only supports HTTP v1.0. In other words, although this class
36
#   is used already, the output is only flushed at the end.
37
#  }
38
# }
39
#
40
# @author
41
#
42
# \seealso{
43
#   @see "HttpDaemon".
44
# }
45
#
46
# @keyword IO
47
# @keyword internal
48
#*/###########################################################################
49
setConstructorS3("HttpDaemonRspResponse", function(httpDaemon=NULL, ...) {
50 1
  if (!is.null(httpDaemon)) {
51 0
    if (!inherits(httpDaemon, "HttpDaemon")) {
52 0
      throw("Argument 'httpDaemon' is not an HttpDaemon object: ",
53 0
                                                       class(httpDaemon)[1])
54
    }
55
  }
56

57 1
  extend(FileRspResponse(), "HttpDaemonRspResponse",
58 1
    .httpDaemon = httpDaemon,
59 1
    .bfr = NULL,
60
    ...
61
  )
62
})
63

64

65
setMethodS3("writeResponse", "HttpDaemonRspResponse", function(this, ..., collapse="", sep="") {
66 0
  version <- getOption("R.rsp/HttpDaemon/RspVersion", "0.1.0")
67
  # Argment 'version':
68 0
  if (!is.element(version, c("0.1.0", "1.0.0"))) {
69 0
    throw("Unknown HttpDaemon RSP version: ", version)
70
  }
71

72
  # String to output
73 0
  msg <- paste(..., collapse=collapse, sep=sep)
74 0
  msg <- as.character(GString(msg))
75

76 0
  if (version == "0.1.0") {
77 0
    this$.bfr <- c(this$.bfr, msg)
78 0
  } else if (version == "1.0.0") {
79 0
    cat(msg)
80
  }
81
})
82

83

84

85
setMethodS3("flush", "HttpDaemonRspResponse", function(con) {
86
  # To please R CMD check.
87 0
  this <- con
88

89
  # Get the content of the buffer
90 0
  bfr <- this$.bfr
91

92 0
  if (is.null(bfr))
93 0
    return(invisible(as.integer(0)))
94

95
  # Write buffer
96 0
  len <- writeResponse(this$.httpDaemon, bfr)
97

98
  # Clear buffer
99 0
  this$.bfr <- NULL
100

101 0
  invisible(len)
102
}, appendVarArgs=FALSE)

Read our documentation on viewing source code .

Loading