Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Generated by roxygen2 (4.0.2): do not edit by hand
# Generated by roxygen2 (4.1.0): do not edit by hand

export(decodeURI)
export(decodeURIComponent)
export(encodeURI)
export(encodeURIComponent)
export(interrupt)
export(rawToBase64)
export(runPipeServer)
export(runServer)
export(service)
export(startDaemonizedServer)
Expand Down
38 changes: 38 additions & 0 deletions R/httpuv.R
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,44 @@ runServer <- function(host, port, app,
}
}

#' Run a server on a unix socket
#'
#' This is a convenience function that provides a simple way to call
#' \code{\link{startPipeServer}}, \code{\link{service}}, and
#' \code{\link{stopServer}} in the correct sequence. It does not return unless
#' interrupted or an error occurs.
#'
#' If you have multiple hosts and/or ports to listen on, call the individual
#' functions instead of \code{runPipeServer}.
#'
#' @param name A string that indicates the path for the domain socket (on
#' Unix-like systems) or the name of the named pipe (on Windows).
#' @param mask If non-\code{NULL} and non-negative, this numeric value is used
#' to temporarily modify the process's umask while the domain socket is being
#' created. To ensure that only root can access the domain socket, use
#' \code{strtoi("777", 8)}; or to allow owner and group read/write access, use
#' \code{strtoi("117", 8)}. If the value is \code{NULL} then the process's
#' umask is left unchanged. (This parameter has no effect on Windows.)
#' @param app A collection of functions that define your application. See
#' \code{\link{startServer}}.
#' @param interruptIntervalMs How often to check for interrupt. The default
#' should be appropriate for most situations.
#'
#' @seealso \code{\link{startPipeServer}}, \code{\link{service}},
#' \code{\link{stopServer}}
#' @export
runPipeServer <- function(name, mask, app,
interruptIntervalMs = ifelse(interactive(), 100, 1000)) {
server <- startPipeServer(name, mask, app)
on.exit(stopServer(server))

.globals$stopped <- FALSE
while (!.globals$stopped) {
service(interruptIntervalMs)
Sys.sleep(0.001)
}
}

#' Interrupt httpuv runloop
#'
#' Interrupts the currently running httpuv runloop, meaning
Expand Down
33 changes: 33 additions & 0 deletions demo/socket-hello.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env Rscript

library(httpuv)

app <- list(
call = function(req) {
list(
status = 200L,
headers = list(
'Content-Type' = 'text/html'
),
body = paste(
sep = "\r\n",
"<!DOCTYPE html>",
"<html>",
"<head>",
'<style type="text/css">',
'body { font-family: Helvetica; }',
'pre { margin: 0 }',
'</style>',
"</head>",
"<body>",
'<h3>Hello World</h3>',
"</body>",
"</html>"
)
)
}
)

runPipeServer("/tmp/httpuv-hello-demo", strtoi("022", 8), app, 250)
# echo -e "GET / HTTP/1.1\r\n" | socat unix-connect:/tmp/httpuv-hello-demo STDIO

41 changes: 41 additions & 0 deletions man/runPipeServer.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/httpuv.R
\name{runPipeServer}
\alias{runPipeServer}
\title{Run a server on a unix socket}
\usage{
runPipeServer(name, mask, app, interruptIntervalMs = ifelse(interactive(),
100, 1000))
}
\arguments{
\item{name}{A string that indicates the path for the domain socket (on
Unix-like systems) or the name of the named pipe (on Windows).}

\item{mask}{If non-\code{NULL} and non-negative, this numeric value is used
to temporarily modify the process's umask while the domain socket is being
created. To ensure that only root can access the domain socket, use
\code{strtoi("777", 8)}; or to allow owner and group read/write access, use
\code{strtoi("117", 8)}. If the value is \code{NULL} then the process's
umask is left unchanged. (This parameter has no effect on Windows.)}

\item{app}{A collection of functions that define your application. See
\code{\link{startServer}}.}

\item{interruptIntervalMs}{How often to check for interrupt. The default
should be appropriate for most situations.}
}
\description{
This is a convenience function that provides a simple way to call
\code{\link{startPipeServer}}, \code{\link{service}}, and
\code{\link{stopServer}} in the correct sequence. It does not return unless
interrupted or an error occurs.
}
\details{
If you have multiple hosts and/or ports to listen on, call the individual
functions instead of \code{runPipeServer}.
}
\seealso{
\code{\link{startPipeServer}}, \code{\link{service}},
\code{\link{stopServer}}
}