From e714a2d4e013df1d9b72c6dec785f026015415cb Mon Sep 17 00:00:00 2001 From: Bob Rudis Date: Tue, 14 Feb 2017 13:15:35 -0500 Subject: [PATCH] helpers --- NAMESPACE | 2 ++ R/helpers.r | 18 ++++++++++++++++++ R/render-har.r | 28 ++++++++++++++++++++++------ README.Rmd | 1 + README.md | 1 + man/as_har.Rd | 14 ++++++++++++++ man/get_content_type.Rd | 3 +++ 7 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 man/as_har.Rd diff --git a/NAMESPACE b/NAMESPACE index 17806b9..517121d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ S3method(print,splash_json) export("%>%") export(HARviewer) export(HARviewerOutput) +export(as_har) export(execute_lua) export(get_body_size) export(get_content_size) @@ -22,6 +23,7 @@ export(is_json) export(is_plain) export(is_png) export(is_svg) +export(is_xhr) export(is_xml) export(renderHARviewer) export(render_file) diff --git a/R/helpers.r b/R/helpers.r index dff33a3..a0a34a1 100644 --- a/R/helpers.r +++ b/R/helpers.r @@ -64,6 +64,24 @@ is_svg <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/sv #' @export is_gif <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/gif") } +#' @rdname get_content_type +#' @export +is_xhr <- function(x) { + + if (is.null(x$request$headers)) return(NA) + if (length(x$request$headers)==0) return(NA) + + y <- map(x$request$headers, "value") + names(y) <- tolower(map_chr(x$request$headers, "name")) + + if ("x-requested-with" %in% names(y)) { + return(tolower("xmlhttprequest") == tolower(y[["x-requested-with"]])) + } else { + return(FALSE) + } + +} + #' Retrieve size of content | body | headers #' #' @param har_resp_obj HAR response object diff --git a/R/render-har.r b/R/render-har.r index a573235..a042087 100644 --- a/R/render-har.r +++ b/R/render-har.r @@ -40,13 +40,28 @@ render_har <- function(splash_obj, url, base_url, response_body=FALSE, timeout=3 out <- httr::content(res, as="text", encoding="UTF-8") spl <- jsonlite::fromJSON(out, flatten=FALSE, simplifyVector=FALSE) + as_har(spl) + +} + + +#' Turn a generic Splash HAR response into a HAR object +#' +#' @param as_har splash response object as returned by `splash:har()` lua script +#' @export +as_har <- function(splash_resp) { + + if (is.raw(splash_resp)) splash_resp <- jsonlite::fromJSON(rawToChar(splash_resp), + flatten=FALSE, + simplifyVector=FALSE) + sphar <- list( log=list( - version=spl$log$version, - creator=spl$log$creator, - browser=spl$log$browser, - pages=spl$log$pages, - entries=spl$log$entries + version=splash_resp$log$version, + creator=splash_resp$log$creator, + browser=splash_resp$log$browser, + pages=splash_resp$log$pages, + entries=splash_resp$log$entries ) ) @@ -67,4 +82,5 @@ render_har <- function(splash_obj, url, base_url, response_body=FALSE, timeout=3 sphar -} + +} \ No newline at end of file diff --git a/README.Rmd b/README.Rmd index 8c8e5b5..3b1bb03 100644 --- a/README.Rmd +++ b/README.Rmd @@ -66,6 +66,7 @@ Helpers: - `is_plain`: Retrieve or test content type of a HAR request object - `is_png`: Retrieve or test content type of a HAR request object - `is_svg`: Retrieve or test content type of a HAR request object +- `is_xhr`: Retrieve or test content type of a HAR request object - `is_xml`: Retrieve or test content type of a HAR request object Some functions from `HARtools` are imported/exported and `%>%` is imported/exported. diff --git a/README.md b/README.md index 5027128..ee7c70f 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ Helpers: - `is_plain`: Retrieve or test content type of a HAR request object - `is_png`: Retrieve or test content type of a HAR request object - `is_svg`: Retrieve or test content type of a HAR request object +- `is_xhr`: Retrieve or test content type of a HAR request object - `is_xml`: Retrieve or test content type of a HAR request object Some functions from `HARtools` are imported/exported and `%>%` is imported/exported. diff --git a/man/as_har.Rd b/man/as_har.Rd new file mode 100644 index 0000000..f245879 --- /dev/null +++ b/man/as_har.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/render-har.r +\name{as_har} +\alias{as_har} +\title{Turn a generic Splash HAR response into a HAR object} +\usage{ +as_har(splash_resp) +} +\arguments{ +\item{as_har}{splash response object as returned by `splash:har()` lua script} +} +\description{ +Turn a generic Splash HAR response into a HAR object +} diff --git a/man/get_content_type.Rd b/man/get_content_type.Rd index 767ca77..db6f412 100644 --- a/man/get_content_type.Rd +++ b/man/get_content_type.Rd @@ -14,6 +14,7 @@ \alias{is_png} \alias{is_svg} \alias{is_gif} +\alias{is_xhr} \title{Retrieve or test content type of a HAR request object} \usage{ get_content_type(har_resp_obj) @@ -41,6 +42,8 @@ is_png(har_resp_obj) is_svg(har_resp_obj) is_gif(har_resp_obj) + +is_xhr(x) } \arguments{ \item{har_resp_obj}{HAR response object}