Blame | Last modification | View Log | Download | RSS feed
\documentclass{article}\usepackage{fullpage}\usepackage{times}\author{Duncan Temple Lang\\Department of Statistics,\\Universy of California at Davis.}\title{RCurl - an R package for HTTP requests}\begin{document}\maketitle\begin{abstract}The Hyper-Text Transfer Protocol (HTTP) is the communicationunderlying much of the Web and Web services, allowing Web browsersto download pages, submit HTML forms, and other applications toperform rich distributed computing via Web Services such as SOAP.The RCurl package provides an interface to a well-established,general and flexible C library -- libcurl --that performs HTTP requests. We describe the primary functionsin the R package and the basic computational model exposed via thisinterface. We describe some of the more advanced and flexible features ofthe package and provide some examples. We contrast it with otherpotential HTTP client packages for R and suggest some potentialimprovements that could be made in the package.\end{abstract}\section{Introduction}In section \ref{HTTPOverview}, we provide an overview of theHyper-Text Transfer Protocol. This helps to illustrate the largenumber of facilities one must implement to provide a complete HTTPclient facility. In section \section \ref{RCurlBasics}, we presentthe primary functionality of the RCurl package and illustrate thesewith some basic examples. In section \ref{AdvancedFeatures}, wedescribe some of the more advanced features of the package that can beused to provide more controlled access to the HTTP requests and theresponses. In the final section, we discuss some alternativeapproaches to providing client-side HTTP facilities in R and somepotential improvements to the RCurl package. The goal of the paper isto provide a flavor for what the RCurl package can do and how to thinkabout it when using it to develop Web-based applications in R. Thepaper is not intended to be a complete programmers reference guide.One should consult the help pages for both RCurl and libcurl fordetailed information about the possible options one can use.\section{The Hyper-Text Transfer Protocol}\label{HTTPOverview}Almost everyone using the Web in some form will be familiar with theterm `http' that is the most common prefix for web sites. This standsfor the Hyper-Text Transfer Protocol and is represents a language orprotocol by which a Web client and server can converse. Defined by aW3 document \cite{HTTPSpec}, it specifies how a client can initiate aconversation with a Web server, how the two can negotiate the settingseach will use, how the client can request specific documents orcontent and how this material is to be transferred. Ths simplest useis the most common and involves downloading a file from a Web server.Let's consider the essentials of what happens when we download the URI(Uniform Resource Identifier) using a Web browser.RedirectsRequest Bodyuploads using boundary stringsErrorsSuppose we were to download the file index.html from the RCurl webpage, i.e. \url{http://www.omegahat.net/RCurl/index.html}. Ourclient would use TCP/IP to connect to the machine www.omegahat.net(having first resolved the IP address of the machine corresponding thename www.omegahat.net). It would attempt to connect on the port 80which is the default port on which Web servers listen for suchrequests. If the server were listening on a non-standard port, suchas 8080, the request would be\url{http://www.omegahat.net:8080/RCurl/index.html} and the connectionwould be established using that different port.Assuming the connectin has been made, the client and serverthen start their HTTP conversation.The client writes the basic command to fetch the file/RCurl/index.html:\begin{verbatim}GET /RCurl/index.html HTTP/1.1\end{verbatim}The word GET tells the server what operation is being requested, inthis case the retrieval of a document. The next part of the commandidentifies the document. And the final part tells the Web server thatthe client wishes to communicate using HTTP rather than any otherprotocol, and specifically version 1.1 of the protocol rather than1.0. This more recent version of the protocol provides enhancedfacilities for the client and server to provide informationcontrolling the connection between them.Because we are using the 1.1 protocol, the client must provideinformation identifying itself the application in addition to thisbasic retrieval instruction. Specifically, it must identify thedomain of the Web server from which it is requesting the document.This might seem strange; surely the Web server knows the domain of theWeb server itself. This is not always the case as a single Web servercan act as a virtual server for many domains. And so, in version 1.1of HTTP, the client should indicate the domain of interest.So the client need only write the following textalong the socket connecting the client and Web server:\begin{verbatim}GET / HTTP/1.1Host: www.omegahat.net\end{verbatim}Both lines are ended with a control-linefeed (\\\\r\\\\n) and therequest is completed by sending a terminating blank-line.The Web server is reading the bytes on the socket and then recognizesthe end of the request. It then processes the request and provides aresponse. Each reply has the same basic form.The first line provides information about therequest. The last component provides an indicationabout the status of the request.The numbers indicate successor failure adn identify particularreasons for that response.The next segment of the responseis a collection of name--value pairsgiven as\begin{verbatim}name: value\end{verbatim}These provide information for the clienttelling it how the informationin the response is encoded.This segment is terminated by a blank lineand then the data for the response appearsis written to the connection.The client can then read this information\begin{figure}[htbp]\begin{center}\leavevmode\caption{The anatomy of an HTTP response}\label{fig:HTTPResponse}\end{center}\end{figure}\section{RCurl and libcurl}\label{RCurlBasics}\subsection{The Basic Computational Model}This describes the basic functions of requesting a URI and submittinga form. It describes how we can have a handle that we can reuseacross multiple requests or create one each time. It discusses theuse of options via the \ldots mechanism to set libcurl options withina call or persistently within a libcurl handle that apply to allrequests using that handle.\subsection{Downloading a URI.}\subsection{Forms}Submitting a form using GET and POST.Google.basic CGI env.wormbase\subssubsection{Discovering the Available Options}\subsection{libcurl and default functionality}\subsubsection{.netrc}\subsubsection{SSL}\section{Advanced Features}\label{AdvancedFeatures}\subsection{Accessing the header information}\subsection{Uploading a File}\section{Alternative Approaches and Future Work}httpRequestnot very complete.Other libraries such as libwww.libcurl has many features and is very portable.Event loop.multi-threaded libcurl.\end{document}