Rev 7946 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{dynCurlReader}\alias{dynCurlReader}%- Also NEED an '\alias' for EACH other topic documented here.\title{Dynamically determine content-type of body from HTTP header andset body reader}\description{This function is used for the \code{writefunction}option in a curl HTTP request.The idea is that we read the header of the HTTP responseand when our code determines that the header is complete(the presence of a blank line), it examines the contentsof the header and finds a Content-Type field.It uses the value of this to determine the nature of thebody of the HTTP response and dynamically (re)sets the readerfor the curl handle appropriately. If the content is binary,it collects the content into a \code{raw} vector;if it is text, it sets the appropriate character encodingand collects the content into a character vector.This function is like \code{\link{basicTextGatherer}}but behaves dynamically by determining how to read the contentbased on the header of the HTTP response.This function returns a list of functions that are usedto update and query a shared state across calls.}\usage{dynCurlReader(curl = getCurlHandle(), txt = character(), max = NA,value = NULL, verbose = FALSE, binary = NA, baseURL = NA,isHTTP = NA, encoding = NA)}%- maybe also 'usage' for other objects documented here.\arguments{\item{curl}{the curl handle to be used for the request. It isessential that this handle be used in the low-level call to\code{\link{curlPerform}} so that the updateelement sets the reader for the body on the appropriatecurl handle that is used in the request.}\item{txt}{initial value of the text. This is almost always an emptycharacter vector.}\item{max}{the maximum number of characters to read. This is almostalways \code{NA}.}\item{value}{a function that can be specified which will be used toconvert the body of the response from text or raw in a customizedmanner,e.g. uncompress a gzip body. This can als be done explicitlywith a call \code{fun(reader$value())} after the body has been read.The advantage of specifying the function in the constructor of thereader is that the end-user doesn't have to know which function touse to do the conversion.}\item{verbose}{a logical value indicating whether messages aboutprogress and operations are written on the console as theheader and body are processed.}\item{binary}{a logical value indicating whether the caller knowswhether the resulting content is binary (\code{TRUE}) or not(\code{FALSE}) or unknown (\code{NA}).}\item{baseURL}{the URL of the request which can be used to followlinks to other URLs that are described relative to this.}\item{isHTTP}{a logical value indicating whether the request/download usesHTTP or not. If this is \code{NA}, we determine this when the headeris received. If the caller knows this is an FTP or other request,they can specify this when creating the reader.}\item{encoding}{a string that allows the caller to specify and overridethe encoding of the result. This is used to convert text returnedfrom the server.}}\value{A list with 5 elements all of which are functions. These are\item{update}{the function that does the actual reading/processing ofthe content that libcurl passes to it from the header and thebody. This is the work-horse of the reader.}\item{value}{a function to get the body of the response}\item{header}{a function to get the content of the HTPP header}\item{reset}{a function to reset the internal contents which allowsthe same reader to be re-used in subsequent HTTP requests}\item{curl}{accessor function for the curl handle specified in thecall to create this dynamic reader object.}This list has the S3 class vector\code{c("DynamicRCurlTextHandler", "RCurlTextHandler", "RCurlCallbackFunction")}}\references{libcurl \url{https://curl.se/}}\author{Duncan Temple Lang}\seealso{\code{\link{basicTextGatherer}}\code{\link{curlPerform}}\code{\link{getURLContent}}}\examples{# Each of these examples can be done with getURLContent().# These are here just to illustrate the dynamic reader.if(url.exists("https://www.omegahat.net/Rcartogram/demo.jpg")) withAutoprint({header = dynCurlReader()curlPerform(url = "https://www.omegahat.net/Rcartogram/demo.jpg",headerfunction = header$update, curl = header$curl())class( header$value() )length( header$value() )})if(url.exists("https://www.omegahat.net/dd.gz")) withAutoprint({# gzip example.header = dynCurlReader()curlPerform(url = "https://www.omegahat.net/dd.gz",headerfunction = header$update, curl = header$curl())class( header$value() )length( header$value() )if (getRversion() >= "4")cat(memDecompress(header$value(), asChar = TRUE))## or cat(Rcompression::gunzip(header$value()))})# Character encoding example\dontrun{header = dynCurlReader()curlPerform(url = "http://www.razorvine.net/test/utf8form/formaccepter.sn",postfields = c(text = "ABC", outputencoding = "UTF-8"),verbose = TRUE,writefunction = header$update, curl = header$curl())class( header$value() )Encoding( header$value() )}}\keyword{IO}\concept{binary}