Title: | HTTP and WebSocket Server Library |
---|---|
Description: | Provides low-level socket and protocol support for handling HTTP and WebSocket requests directly from within R. It is primarily intended as a building block for other packages, rather than making it particularly easy to create complete web applications using httpuv alone. httpuv is built on top of the libuv and http-parser C libraries, both of which were developed by Joyent, Inc. (See LICENSE file for libuv and http-parser license information.) |
Authors: | Joe Cheng [aut], Winston Chang [aut, cre], Posit, PBC fnd [cph], Hector Corrada Bravo [ctb], Jeroen Ooms [ctb], Andrzej Krzemienski [cph] (optional.hpp), libuv project contributors [cph] (libuv library, see src/libuv/AUTHORS file), Joyent, Inc. and other Node contributors [cph] (libuv library, see src/libuv/AUTHORS file; and http-parser library, see src/http-parser/AUTHORS file), Niels Provos [cph] (libuv subcomponent: tree.h), Internet Systems Consortium, Inc. [cph] (libuv subcomponent: inet_pton and inet_ntop, contained in src/libuv/src/inet.c), Alexander Chemeris [cph] (libuv subcomponent: stdint-msvc2008.h (from msinttypes)), Google, Inc. [cph] (libuv subcomponent: pthread-fixes.c), Sony Mobile Communcations AB [cph] (libuv subcomponent: pthread-fixes.c), Berkeley Software Design Inc. [cph] (libuv subcomponent: android-ifaddrs.h, android-ifaddrs.c), Kenneth MacKay [cph] (libuv subcomponent: android-ifaddrs.h, android-ifaddrs.c), Emergya (Cloud4all, FP7/2007-2013, grant agreement no 289016) [cph] (libuv subcomponent: android-ifaddrs.h, android-ifaddrs.c), Steve Reid [aut] (SHA-1 implementation), James Brown [aut] (SHA-1 implementation), Bob Trower [aut] (base64 implementation), Alexander Peslyak [aut] (MD5 implementation), Trantor Standard Systems [cph] (base64 implementation), Igor Sysoev [cph] (http-parser) |
Maintainer: | Winston Chang <[email protected]> |
License: | GPL (>= 2) | file LICENSE |
Version: | 1.6.15.9000 |
Built: | 2024-12-02 05:13:32 UTC |
Source: | https://github.com/rstudio/httpuv |
HTTP and WebSocket server
Allows R code to listen for and interact with HTTP and WebSocket clients, so you can serve web traffic directly out of your R process. Implementation is based on libuv and http-parser.
This is a low-level library that provides little more than network I/O and implementations of the HTTP and WebSocket protocols. For an easy way to create web applications, try Shiny instead.
Joe Cheng [email protected]
## Not run: demo("echo", package="httpuv") ## End(Not run)
## Not run: demo("echo", package="httpuv") ## End(Not run)
Encodes/decodes strings using URI encoding/decoding in the same way that web browsers do. The precise behaviors of these functions can be found at developer.mozilla.org: encodeURI, encodeURIComponent, decodeURI, decodeURIComponent
encodeURI(value) encodeURIComponent(value) decodeURI(value) decodeURIComponent(value)
encodeURI(value) encodeURIComponent(value) decodeURI(value) decodeURIComponent(value)
value |
Character vector to be encoded or decoded. |
Intended as a faster replacement for utils::URLencode()
and
utils::URLdecode()
.
encodeURI differs from encodeURIComponent in that the former will not encode
reserved characters: ;,/?:@&=+$
decodeURI differs from decodeURIComponent in that it will refuse to decode encoded sequences that decode to a reserved character. (If in doubt, use decodeURIComponent.)
For encodeURI
and encodeURIComponent
, input strings will be
converted to UTF-8 before URL-encoding.
Encoded or decoded character vector of the same length as the
input value. decodeURI
and decodeURIComponent
will return
strings that are UTF-8 encoded.
Interrupts the currently running httpuv runloop, meaning
runServer
or service
will return control back to
the caller and no further tasks will be processed until those methods are
called again. Note that this may cause in-process uploads or downloads to be
interrupted in mid-request.
interrupt()
interrupt()
Given an IP address, this checks whether it is an IPv4 or IPv6 address.
ipFamily(ip)
ipFamily(ip)
ip |
A single string representing an IP address. |
For IPv4 addresses, 4
; for IPv6 addresses, 6
. If the address is
neither, -1
.
ipFamily("127.0.0.1") # 4 ipFamily("500.0.0.500") # -1 ipFamily("500.0.0.500") # -1 ipFamily("::") # 6 ipFamily("::1") # 6 ipFamily("fe80::1ff:fe23:4567:890a") # 6
ipFamily("127.0.0.1") # 4 ipFamily("500.0.0.500") # -1 ipFamily("500.0.0.500") # -1 ipFamily("::") # 6 ipFamily("::1") # 6 ipFamily("fe80::1ff:fe23:4567:890a") # 6
This returns a list of all running httpuv server applications.
listServers()
listServers()
Finds a random available TCP port for listening on, within a specified range of ports. The default range of ports to check is 1024 to 49151, which is the set of TCP User Ports. This function automatically excludes some ports which are considered unsafe by web browsers.
randomPort(min = 1024L, max = 49151L, host = "127.0.0.1", n = 20)
randomPort(min = 1024L, max = 49151L, host = "127.0.0.1", n = 20)
min |
Minimum port number. |
max |
Maximum port number. |
host |
A string that is a valid IPv4 or IPv6 address that is owned by
this server, which the application will listen on. |
n |
Number of ports to try before giving up. |
A port that is available to listen on.
## Not run: s <- startServer("127.0.0.1", randomPort(), list()) browseURL(paste0("http://127.0.0.1:", s$getPort())) s$stop() ## End(Not run)
## Not run: s <- startServer("127.0.0.1", randomPort(), list()) browseURL(paste0("http://127.0.0.1:", s$getPort())) s$stop() ## End(Not run)
Converts a raw vector to its Base64 encoding as a single-element character vector.
rawToBase64(x)
rawToBase64(x)
x |
A raw vector. |
set.seed(100) result <- rawToBase64(as.raw(runif(19, min=0, max=256))) stopifnot(identical(result, "TkGNDnd7z16LK5/hR2bDqzRbXA=="))
set.seed(100) result <- rawToBase64(as.raw(runif(19, min=0, max=256))) stopifnot(identical(result, "TkGNDnd7z16LK5/hR2bDqzRbXA=="))
This is a convenience function that provides a simple way to call
startServer
, service
, and
stopServer
in the correct sequence. It does not return unless
interrupted or an error occurs.
runServer(host, port, app, interruptIntervalMs = NULL)
runServer(host, port, app, interruptIntervalMs = NULL)
host |
A string that is a valid IPv4 or IPv6 address that is owned by
this server, which the application will listen on. |
port |
A number or integer that indicates the server port that should be listened on. Note that on most Unix-like systems including Linux and macOS, port numbers smaller than 1024 require root privileges. |
app |
A collection of functions that define your application. See
|
interruptIntervalMs |
Deprecated (last used in httpuv 1.3.5). |
If you have multiple hosts and/or ports to listen on, call the individual
functions instead of runServer
.
startServer
, service
,
stopServer
## Not run: # A very basic application runServer("0.0.0.0", 5000, list( call = function(req) { list( status = 200L, headers = list( 'Content-Type' = 'text/html' ), body = "Hello world!" ) } ) ) ## End(Not run)
## Not run: # A very basic application runServer("0.0.0.0", 5000, list( call = function(req) { list( status = 200L, headers = list( 'Content-Type' = 'text/html' ), body = "Hello world!" ) } ) ) ## End(Not run)
runStaticServer()
provides a convenient interface to start a server to host
a single static directory, either in the foreground or the background.
runStaticServer( dir = getwd(), host = "127.0.0.1", port = NULL, ..., background = FALSE, browse = interactive() )
runStaticServer( dir = getwd(), host = "127.0.0.1", port = NULL, ..., background = FALSE, browse = interactive() )
dir |
The directory to serve. Defaults to the current working directory. |
host |
A string that is a valid IPv4 address that is owned by this
server, or |
port |
A number or integer that indicates the server port that should be listened on. Note that on most Unix-like systems including Linux and macOS, port numbers smaller than 1024 require root privileges. |
... |
Arguments passed on to
|
background |
Whether to run the server in the background. By default,
the server runs in the foreground and blocks the R console. You can stop
the server by interrupting it with When |
browse |
Whether to automatically open the served directory in a web
browser. Defaults to |
Starts a server on the specified host and port. By default the
server runs in the foreground and is accessible at http://127.0.0.1:7446
.
When background = TRUE
, the server
object is returned invisibly.
runServer()
provides a similar interface for running a dynamic
app server. Both runStaticServer()
and runServer()
are built on top of
startServer()
, service()
and stopServer()
. Learn more about httpuv
servers in startServer()
.
website_dir <- system.file("example-static-site", package = "httpuv") runStaticServer(dir = website_dir)
website_dir <- system.file("example-static-site", package = "httpuv") runStaticServer(dir = website_dir)
Process HTTP requests and WebSocket messages. If there is nothing on R's call
stack – if R is sitting idle at the command prompt – it is not necessary to
call this function, because requests will be handled automatically. However,
if R is executing code, then requests will not be handled until either the
call stack is empty, or this function is called (or alternatively,
run_now
is called).
service(timeoutMs = ifelse(interactive(), 100, 1000))
service(timeoutMs = ifelse(interactive(), 100, 1000))
timeoutMs |
Approximate number of milliseconds to run before returning. It will return this duration has elapsed. If 0 or Inf, then the function will continually process requests without returning unless an error occurs. If NA, performs a non-blocking run without waiting. |
In previous versions of httpuv (1.3.5 and below), even if a server created by
startServer
exists, no requests were serviced unless and until
service
was called.
This function simply calls run_now()
, so if your
application schedules any later
callbacks, they will be
invoked.
## Not run: while (TRUE) { service() } ## End(Not run)
## Not run: while (TRUE) { service() } ## End(Not run)
This function will be removed in a future release of httpuv. It is simply a
wrapper for startServer
. In previous versions of httpuv (1.3.5
and below), startServer
ran applications in the foreground and
startDaemonizedServer
ran applications in the background, but now both
of them run applications in the background.
startDaemonizedServer(host, port, app, quiet = FALSE)
startDaemonizedServer(host, port, app, quiet = FALSE)
host |
A string that is a valid IPv4 address that is owned by this
server, or |
port |
A number or integer that indicates the server port that should be listened on. Note that on most Unix-like systems including Linux and macOS, port numbers smaller than 1024 require root privileges. |
app |
A collection of functions that define your application. See Details. |
quiet |
If |
Creates an HTTP/WebSocket server on the specified host and port.
startServer(host, port, app, quiet = FALSE) startPipeServer(name, mask, app, quiet = FALSE)
startServer(host, port, app, quiet = FALSE) startPipeServer(name, mask, app, quiet = FALSE)
host |
A string that is a valid IPv4 address that is owned by this
server, or |
port |
A number or integer that indicates the server port that should be listened on. Note that on most Unix-like systems including Linux and macOS, port numbers smaller than 1024 require root privileges. |
app |
A collection of functions that define your application. See Details. |
quiet |
If |
name |
A string that indicates the path for the domain socket (on Unix-like systems) or the name of the named pipe (on Windows). |
mask |
If non- |
startServer
binds the specified port and listens for
connections on an thread running in the background. This background thread
handles the I/O, and when it receives a HTTP request, it will schedule a
call to the user-defined R functions in app
to handle the request.
This scheduling is done with later()
. When the R call
stack is empty – in other words, when an interactive R session is sitting
idle at the command prompt – R will automatically run the scheduled calls.
However, if the call stack is not empty – if R is evaluating other R code
– then the callbacks will not execute until either the call stack is
empty, or the run_now()
function is called. This
function tells R to execute any callbacks that have been scheduled by
later()
. The service()
function is
essentially a wrapper for run_now()
.
In older versions of httpuv (1.3.5 and below), it did not use a background
thread for I/O, and when this function was called, it did not accept
connections immediately. It was necessary to call service
repeatedly in order to actually accept and handle connections.
If the port cannot be bound (most likely due to permissions or because it is already bound), an error is raised.
The application can also specify paths on the filesystem which will be
served from the background thread, without invoking $call()
or
$onHeaders()
. Files served this way will be only use a C++ code,
which is faster than going through R, and will not be blocked when R code
is executing. This can greatly improve performance when serving static
assets.
The app
parameter is where your application logic will be provided
to the server. This can be a list, environment, or reference class that
contains the following methods and fields:
call(req)
Process the given HTTP request, and return an
HTTP response (see Response Values). This method should be implemented in
accordance with the
Rook
specification. Note that httpuv augments req
with an additional
item, req$HEADERS
, which is a named character vector of request
headers.
onHeaders(req)
Optional. Similar to call
, but occurs
when headers are received. Return NULL
to continue normal
processing of the request, or a Rook response to send that response,
stop processing the request, and ask the client to close the connection.
(This can be used to implement upload size limits, for example.)
onWSOpen(ws)
Called back when a WebSocket connection is established.
The given object can be used to be notified when a message is received from
the client, to send messages to the client, etc. See WebSocket
.
staticPaths
A named list of paths that will be served without invoking
call()
or onHeaders
. The name of each one is the URL
path, and the value is either a string referring to a local path, or an
object created by the staticPath
function.
staticPathOptions
A set of default options to use when serving static paths. If
not set or NULL
, then it will use the result from calling
staticPathOptions()
with no arguments.
The startPipeServer
variant can be used instead of
startServer
to listen on a Unix domain socket or named pipe rather
than a TCP socket (this is not common).
A handle for this server that can be passed to
stopServer
to shut the server down.
A WebServer
or PipeServer
object.
The call
function is expected to return a list containing the
following, which are converted to an HTTP response and sent to the client:
status
A numeric HTTP status code, e.g. 200
or
404L
.
headers
A named list of HTTP headers and their values, as
strings. This can also be missing, an empty list, or NULL
, in which
case no headers (other than the Date
and Content-Length
headers, as required) will be added.
body
A string (or raw
vector) to be sent as the body
of the HTTP response. This can also be omitted or set to NULL
to
avoid sending any body, which is useful for HTTP 1xx
, 204
,
and 304
responses, as well as responses to HEAD
requests.
stopServer
, runServer
,
listServers
, stopAllServers
.
## Not run: # A very basic application s <- startServer("0.0.0.0", 5000, list( call = function(req) { list( status = 200L, headers = list( 'Content-Type' = 'text/html' ), body = "Hello world!" ) } ) ) s$stop() # An application that serves static assets at the URL paths /assets and /lib s <- startServer("0.0.0.0", 5000, list( call = function(req) { list( status = 200L, headers = list( 'Content-Type' = 'text/html' ), body = "Hello world!" ) }, staticPaths = list( "/assets" = "content/assets/", "/lib" = staticPath( "content/lib", indexhtml = FALSE ), # This subdirectory of /lib should always be handled by the R code path "/lib/dynamic" = excludeStaticPath() ), staticPathOptions = staticPathOptions( indexhtml = TRUE ) ) ) s$stop() ## End(Not run)
## Not run: # A very basic application s <- startServer("0.0.0.0", 5000, list( call = function(req) { list( status = 200L, headers = list( 'Content-Type' = 'text/html' ), body = "Hello world!" ) } ) ) s$stop() # An application that serves static assets at the URL paths /assets and /lib s <- startServer("0.0.0.0", 5000, list( call = function(req) { list( status = 200L, headers = list( 'Content-Type' = 'text/html' ), body = "Hello world!" ) }, staticPaths = list( "/assets" = "content/assets/", "/lib" = staticPath( "content/lib", indexhtml = FALSE ), # This subdirectory of /lib should always be handled by the R code path "/lib/dynamic" = excludeStaticPath() ), staticPathOptions = staticPathOptions( indexhtml = TRUE ) ) ) s$stop() ## End(Not run)
The staticPath
function creates a staticPath
object. Note that
if any of the arguments (other than path
) are NULL
, then that
means that for this particular static path, it should inherit the behavior
from the staticPathOptions set for the application as a whole.
staticPath( path, indexhtml = NULL, fallthrough = NULL, html_charset = NULL, headers = NULL, validation = NULL ) excludeStaticPath()
staticPath( path, indexhtml = NULL, fallthrough = NULL, html_charset = NULL, headers = NULL, validation = NULL ) excludeStaticPath()
path |
The local path. |
indexhtml |
If an index.html file is present, should it be served up when the client requests the static path or any subdirectory? |
fallthrough |
With the default value, |
html_charset |
When HTML files are served, the value that will be
provided for |
headers |
Additional headers and values that will be included in the response. |
validation |
An optional validation pattern. Presently, the only type of
validation supported is an exact string match of a header. For example, if
|
The excludeStaticPath
function tells the application to ignore a
particular path for static serving. This is useful when you want to include a
path for static serving (like "/"
) but then exclude a subdirectory of
it (like "/dynamic"
) so that the subdirectory will always be passed to
the R code for handling requests. excludeStaticPath
can be used not
only for directories; it can also exclude specific files.
Create options for static paths
staticPathOptions( indexhtml = TRUE, fallthrough = FALSE, html_charset = "utf-8", headers = list(), validation = character(0), exclude = FALSE )
staticPathOptions( indexhtml = TRUE, fallthrough = FALSE, html_charset = "utf-8", headers = list(), validation = character(0), exclude = FALSE )
indexhtml |
If an index.html file is present, should it be served up when the client requests the static path or any subdirectory? |
fallthrough |
With the default value, |
html_charset |
When HTML files are served, the value that will be
provided for |
headers |
Additional headers and values that will be included in the response. |
validation |
An optional validation pattern. Presently, the only type of
validation supported is an exact string match of a header. For example, if
|
exclude |
Should this path be excluded from static serving? (This is
only to be used internally, for |
This will stop all applications which were created by
startServer
or startPipeServer
.
stopAllServers()
stopAllServers()
stopServer
to stop a specific server.
This function will be removed in a future release of httpuv. Instead, use
stopServer
.
stopDaemonizedServer(server)
stopDaemonizedServer(server)
server |
A server object that was previously returned from
|
Given a server object that was returned from a previous invocation of
startServer
or startPipeServer
, this closes all
open connections for that server and unbinds the port.
stopServer(server)
stopServer(server)
server |
A server object that was previously returned from
|
stopAllServers
to stop all servers.
A WebSocket
object represents a single WebSocket connection. The
object can be used to send messages and close the connection, and to receive
notifications when messages are received or the connection is closed.
Note that this WebSocket class is different from the one provided by the package named websocket. This class is meant to be used on the server side, whereas the one in the websocket package is to be used as a client. The WebSocket class in httpuv has an older API than the one in the websocket package.
WebSocket objects should never be created directly. They are obtained by
passing an onWSOpen
function to startServer
.
request
The Rook request environment that opened the connection. This can be used to inspect HTTP headers, for example.
onMessage(func)
Registers a callback function that will be invoked whenever a message
is received on this connection. The callback function will be invoked
with two arguments. The first argument is TRUE
if the message
is binary and FALSE
if it is text. The second argument is either
a raw vector (if the message is binary) or a character vector.
onClose(func)
Registers a callback function that will be invoked when the connection is closed.
send(message)
Begins sending the given message over the websocket. The message must be either a raw vector, or a single-element character vector that is encoded in UTF-8.
close()
Closes the websocket connection.
new()
WebSocket$new(handle, req)
onMessage()
WebSocket$onMessage(func)
onClose()
WebSocket$onClose(func)
send()
WebSocket$send(message)
close()
WebSocket$close(code = 1000L, reason = "")
clone()
The objects of this class are cloneable with this method.
WebSocket$clone(deep = FALSE)
deep
Whether to make a deep clone.
## Not run: # A WebSocket echo server that listens on port 8080 startServer("0.0.0.0", 8080, list( onHeaders = function(req) { # Print connection headers cat(capture.output(str(as.list(req))), sep = "\n") }, onWSOpen = function(ws) { cat("Connection opened.\n") ws$onMessage(function(binary, message) { cat("Server received message:", message, "\n") ws$send(message) }) ws$onClose(function() { cat("Connection closed.\n") }) } ) ) ## End(Not run)
## Not run: # A WebSocket echo server that listens on port 8080 startServer("0.0.0.0", 8080, list( onHeaders = function(req) { # Print connection headers cat(capture.output(str(as.list(req))), sep = "\n") }, onWSOpen = function(ws) { cat("Connection opened.\n") ws$onMessage(function(binary, message) { cat("Server received message:", message, "\n") ws$send(message) }) ws$onClose(function() { cat("Connection closed.\n") }) } ) ) ## End(Not run)