Package webutils
October 30, 2014
Type Package
Title Utility Functions for Web Applications
Version 0.2
Date 2014-10-29
Author Jeroen Ooms
Maintainer Jeroen Ooms <jeroen.ooms@stat.ucla.edu>
Description Utility functions for developing web applications. Includes parsers
for application/x-www-form-urlencoded as well as multipart/form-data
and examples of using the parser with either httpuv or rhttpd.
License MIT + file LICENSE
URL https://github.com/jeroenooms/webutils
BugReports https://github.com/jeroenooms/webutils/issues
Imports tools, utils, jsonlite
Suggests httpuv
NeedsCompilation no
Repository CRAN
Date/Publication 2014-10-30 07:46:54
R topics documented:
demo_httpuv .
demo_rhttpd . .
parse_http . . .
parse_multipart
parse_query . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Index
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2
2
2
3
4
5
parse_http
demo_httpuv
Demo multipart parser with httpuv
Description
Starts the httpuv web server and hosts a simple form including a file upload to demo the multipart
parser.
Usage
demo_httpuv(port)
Arguments
port
demo_rhttpd
which port number to run the http server
Demo multipart parser with rhttpd
Description
Starts the Rhttpd web server and hosts a simple form including a file upload to demo the multipart
parser.
Usage
demo_rhttpd()
parse_http
Parse http request
Description
Parse the body of a http request, based on the Content-Type request header. Currently supports
the three most important content types: application/x-www-form-urlencoded (parse_query),
multipart/form-data (parse_multipart) and application/json (fromJSON).
Usage
parse_http(body, content_type, ...)
parse_multipart
Arguments
body
request body of the http request
content_type
content-type http request header as specified by the client
...
additional arguments passed to parser function
Examples
# Parse json encoded payload:
parse_http('{"foo":123, "bar":true}', 'application/json')
# Parse url-encoded payload
parse_http("foo=1%2B1%3D2&bar=yin%26yang", "application/x-www-form-urlencoded")
## Not run: use demo app to parse multipart/form-data payload
demo_rhttpd()
## End(Not run)
parse_multipart
Parse a multipart/form-data request
Description
Parse a multipart/form-data request, which is usually generated from a HTML form submission.
The parameters can include both text values as well as binary files. They can be distinguished from
the presence of a filename attribute.
Usage
parse_multipart(body, boundary)
Arguments
body
body of the HTTP request. Must be raw or character vector.
boundary
boundary string as specified in the Content-Type request header.
Details
A multipart/form-data request consists of a single body which contains one or more values plus
meta-data, separated using a boundary string. This boundary string is chosen by the client (e.g. the
browser) and specified in the Content-Type header of the HTTP request. There is no escaping; it
is up to the client to choose a boundary string that does not appear in one of the values.
The parser is written in pure R, but still pretty fast because it uses the regex engine.
parse_query
Examples
## Not run: example form
demo_rhttpd()
## End(Not run)
parse_query
Parse query string
Description
Parse http parameters from a query string. This includes unescaping of url-encoded values.
Usage
parse_query(query)
Arguments
query
a url-encoded query string
Details
For http GET requests, the query string is specified in the URL after the question mark. For http
POST or PUT requests, the query string can be used in the request body when the Content-Type
header is set to application/x-www-form-urlencoded.
Examples
q <- "foo=1%2B1%3D2&bar=yin%26yang"
parse_query(q)
Index
demo_httpuv, 2
demo_rhttpd, 2
fromJSON, 2
parse_http, 2
parse_multipart, 2, 3
parse_query, 2, 4