Skip to content

Commit

Permalink
Add support for HTTP HEAD requests
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Aug 5, 2015
1 parent 241a482 commit 0ac8793
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Depends:
methods
Imports:
utils,
httpuv (>= 1.3.2),
httpuv (>= 1.3.2.9001),
mime (>= 0.3),
jsonlite (>= 0.9.16),
xtable,
Expand Down
27 changes: 24 additions & 3 deletions R/middleware.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ HandlerManager <- R6Class("HandlerManager",
headers=list('Content-Type' = 'text/html')))
}

# Catch HEAD requests. For the purposes of handler functions, they
# should be treated like GET. The difference is that they shouldn't
# return a body in the http response.
head_request <- FALSE
if (identical(req$REQUEST_METHOD, "HEAD")) {
head_request <- TRUE
req$REQUEST_METHOD <- "GET"
}

response <- handler(req)
if (is.null(response))
response <- httpResponse(404, content="<h1>Not Found</h1>")
Expand All @@ -341,9 +350,21 @@ HandlerManager <- R6Class("HandlerManager",
headers$'Content-Type' <- response$content_type

response <- filter(req, response)
return(list(status=response$status,
body=response$content,
headers=headers))
if (head_request) {
headers$`Content-Length` <- nchar(response$content, type = "bytes")
return(list(
status = response$status,
body = "",
headers = headers
))
} else {
return(list(
status = response$status,
body = response$content,
headers = headers
))
}

} else {
# Assume it's a Rook-compatible response
return(response)
Expand Down

0 comments on commit 0ac8793

Please sign in to comment.