phrender 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed3ad1d4b44735e82d185bca486adb853fb15fba
4
- data.tar.gz: 1f37c26795b0cacf96926f1a3ebae75f41b15f4b
3
+ metadata.gz: 2c071fb71537894c6506cda8fdf3107307aff153
4
+ data.tar.gz: 1db4ff01dbba039dd67fce848435dfbfc82c8f9e
5
5
  SHA512:
6
- metadata.gz: 9b1dbdae233a0fb2754378696a7757beee1c2ee197cc5aa5a0dac36be76be5b867d9649688c24c23f84b80dbf9a1ea269dc7b113085c1b0fd09459721c65b7ee
7
- data.tar.gz: 42bbbaac4dd4aedae6504f5c972b02794dab825a368e00ec8b88accb65a6cc667dbb87ed2ac29ca9fcbde96c341d34a16ad0b96f9f71602272695ba5861da262
6
+ metadata.gz: 6706612f43d4d98a2eca5f8d5479aef880a933cd90a03b2309ad251c52896afbf3e4e1e4aef6730a9edea83fe8e312ab817a74bb4aadefdbe9f679d0710802e4
7
+ data.tar.gz: 7d7d8093472e1b8d49a6be861a900458072129fae8789672f2f13056ccb334ef35ec178ce5a23f1a7a17339412c28d5994934548a576df4b5ed0cd5240fe5933
@@ -12,13 +12,23 @@ class Phrender::RackMiddleware
12
12
  end
13
13
 
14
14
  def call(env)
15
- status, headers, body = @app.call(env)
16
- if (status == 404 || headers['Content-Type'] == 'text/html')
17
- if (env['HTTP_USER_AGENT'].match(/PhantomJS/))
15
+ @req = Rack::Request.new(env)
16
+
17
+ # Check if the next middleware can handle the request
18
+ status, headers, body = @app.call(@req.env)
19
+
20
+ # If it can't, or if it's just the index file delivered via aliasing for
21
+ # pushstate, do phrender stuff.
22
+ if (status == 404 || headers['Push-State-Redirect'])
23
+ # If it's phantom making the request, then the phrender index file has
24
+ # a request that the upstream server can't resolve, so catch it, instead
25
+ # of recursively invoking the index
26
+ if (@req.user_agent.match(/PhantomJS/))
18
27
  [ 500, { 'Content-Type' => 'text/html' }, [
19
28
  'Server Error: HTML file contains recursive lookup' ] ]
20
29
  else
21
- body = render(env['REQUEST_URI'])
30
+ # Render the page
31
+ body = render(@req.url)
22
32
  [ 200, { 'Content-Type' => 'text/html' }, [ body ] ]
23
33
  end
24
34
  else
@@ -35,11 +45,16 @@ class Phrender::RackMiddleware
35
45
  end
36
46
 
37
47
  def load_html
38
- req = Rack::MockRequest.env_for('',
39
- 'PATH_INFO' => @index_file,
40
- 'REQUEST_METHOD' => 'GET'
41
- )
42
- status, headers, body = @app.call(req)
48
+ req = @req.dup
49
+ req.path_info = @index_file
50
+
51
+ # Attach a param to indicate that it's phrender requesting the page. This is
52
+ # only useful if the index file is delivered via a dynamic backend. Ignored
53
+ # otherwise.
54
+ req.update_param('phrender', true)
55
+ req.env['REQUEST_METHOD'] = 'GET'
56
+
57
+ status, headers, body = @app.call(req.env)
43
58
  parse_body body
44
59
  end
45
60
 
@@ -48,11 +63,11 @@ class Phrender::RackMiddleware
48
63
  if path == :ember_driver
49
64
  Phrender::EMBER_DRIVER
50
65
  else
51
- req = Rack::MockRequest.env_for('',
52
- 'PATH_INFO' => path,
53
- 'REQUEST_METHOD' => 'GET'
54
- )
55
- status, headers, body = @app.call(req)
66
+ req = @req.dup
67
+ req.path_info = path
68
+ req.env['REQUEST_METHOD'] = 'GET'
69
+
70
+ status, headers, body = @app.call(req.env)
56
71
  parse_body body
57
72
  end
58
73
  end.join(';')
@@ -61,6 +76,9 @@ class Phrender::RackMiddleware
61
76
  end
62
77
 
63
78
  def parse_body(body)
79
+ # Rack responses must respond to each, which is generally a polyfil that
80
+ # yields the response, so reassemble it here, or just treat it like a
81
+ # string.
64
82
  if body.respond_to? :each
65
83
  data = ''
66
84
  body.each{ |part| data << part }
@@ -1,3 +1,3 @@
1
1
  class Phrender
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phrender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - M Smart, theScore Inc.