boB Rudis 3 years ago
parent
commit
e714a2d4e0
7 changed files with 61 additions and 6 deletions
  1. 2
    0
      NAMESPACE
  2. 18
    0
      R/helpers.r
  3. 22
    6
      R/render-har.r
  4. 1
    0
      README.Rmd
  5. 1
    0
      README.md
  6. 14
    0
      man/as_har.Rd
  7. 3
    0
      man/get_content_type.Rd

+ 2
- 0
NAMESPACE View File

@@ -5,6 +5,7 @@ S3method(print,splash_json)
5 5
 export("%>%")
6 6
 export(HARviewer)
7 7
 export(HARviewerOutput)
8
+export(as_har)
8 9
 export(execute_lua)
9 10
 export(get_body_size)
10 11
 export(get_content_size)
@@ -22,6 +23,7 @@ export(is_json)
22 23
 export(is_plain)
23 24
 export(is_png)
24 25
 export(is_svg)
26
+export(is_xhr)
25 27
 export(is_xml)
26 28
 export(renderHARviewer)
27 29
 export(render_file)

+ 18
- 0
R/helpers.r View File

@@ -64,6 +64,24 @@ is_svg <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="image/sv
64 64
 #' @export
65 65
 is_gif <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="image/gif") }
66 66
 
67
+#' @rdname get_content_type
68
+#' @export
69
+is_xhr <- function(x) {
70
+
71
+  if (is.null(x$request$headers)) return(NA)
72
+  if (length(x$request$headers)==0) return(NA)
73
+
74
+  y <- map(x$request$headers, "value")
75
+  names(y) <- tolower(map_chr(x$request$headers, "name"))
76
+
77
+  if ("x-requested-with" %in% names(y)) {
78
+    return(tolower("xmlhttprequest") == tolower(y[["x-requested-with"]]))
79
+  } else {
80
+    return(FALSE)
81
+  }
82
+
83
+}
84
+
67 85
 #' Retrieve size of content |  body | headers
68 86
 #'
69 87
 #' @param har_resp_obj HAR response object

+ 22
- 6
R/render-har.r View File

@@ -40,13 +40,28 @@ render_har <- function(splash_obj, url, base_url, response_body=FALSE, timeout=3
40 40
   out <- httr::content(res, as="text", encoding="UTF-8")
41 41
   spl <- jsonlite::fromJSON(out, flatten=FALSE, simplifyVector=FALSE)
42 42
 
43
+  as_har(spl)
44
+
45
+}
46
+
47
+
48
+#' Turn a generic Splash HAR response into a HAR object
49
+#'
50
+#' @param as_har splash response object as returned by `splash:har()` lua script
51
+#' @export
52
+as_har <- function(splash_resp) {
53
+
54
+  if (is.raw(splash_resp)) splash_resp <- jsonlite::fromJSON(rawToChar(splash_resp),
55
+                                                             flatten=FALSE,
56
+                                                             simplifyVector=FALSE)
57
+
43 58
   sphar <- list(
44 59
     log=list(
45
-      version=spl$log$version,
46
-      creator=spl$log$creator,
47
-      browser=spl$log$browser,
48
-      pages=spl$log$pages,
49
-      entries=spl$log$entries
60
+      version=splash_resp$log$version,
61
+      creator=splash_resp$log$creator,
62
+      browser=splash_resp$log$browser,
63
+      pages=splash_resp$log$pages,
64
+      entries=splash_resp$log$entries
50 65
     )
51 66
   )
52 67
 
@@ -67,4 +82,5 @@ render_har <- function(splash_obj, url, base_url, response_body=FALSE, timeout=3
67 82
 
68 83
   sphar
69 84
 
70
-}
85
+
86
+}

+ 1
- 0
README.Rmd View File

@@ -66,6 +66,7 @@ Helpers:
66 66
 - `is_plain`:	Retrieve or test content type of a HAR request object
67 67
 - `is_png`:	Retrieve or test content type of a HAR request object
68 68
 - `is_svg`:	Retrieve or test content type of a HAR request object
69
+- `is_xhr`: Retrieve or test content type of a HAR request object
69 70
 - `is_xml`:	Retrieve or test content type of a HAR request object
70 71
 
71 72
 Some functions from `HARtools` are imported/exported and `%>%` is imported/exported.

+ 1
- 0
README.md View File

@@ -63,6 +63,7 @@ Helpers:
63 63
 -   `is_plain`:	Retrieve or test content type of a HAR request object
64 64
 -   `is_png`:	Retrieve or test content type of a HAR request object
65 65
 -   `is_svg`:	Retrieve or test content type of a HAR request object
66
+-   `is_xhr`: Retrieve or test content type of a HAR request object
66 67
 -   `is_xml`:	Retrieve or test content type of a HAR request object
67 68
 
68 69
 Some functions from `HARtools` are imported/exported and `%>%` is imported/exported.

+ 14
- 0
man/as_har.Rd View File

@@ -0,0 +1,14 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/render-har.r
3
+\name{as_har}
4
+\alias{as_har}
5
+\title{Turn a generic Splash HAR response into a HAR object}
6
+\usage{
7
+as_har(splash_resp)
8
+}
9
+\arguments{
10
+\item{as_har}{splash response object as returned by `splash:har()` lua script}
11
+}
12
+\description{
13
+Turn a generic Splash HAR response into a HAR object
14
+}

+ 3
- 0
man/get_content_type.Rd View File

@@ -14,6 +14,7 @@
14 14
 \alias{is_png}
15 15
 \alias{is_svg}
16 16
 \alias{is_gif}
17
+\alias{is_xhr}
17 18
 \title{Retrieve or test content type of a HAR request object}
18 19
 \usage{
19 20
 get_content_type(har_resp_obj)
@@ -41,6 +42,8 @@ is_png(har_resp_obj)
41 42
 is_svg(har_resp_obj)
42 43
 
43 44
 is_gif(har_resp_obj)
45
+
46
+is_xhr(x)
44 47
 }
45 48
 \arguments{
46 49
 \item{har_resp_obj}{HAR response object}

Loading…
Cancel
Save