Class: MwrapRack

Inherits:
Object
  • Object
show all
Defined in:
lib/mwrap_rack.rb

Overview

MwrapRack is a standalone Rack application which can be mounted to run within your application process.

Using the Rack::Builder API in config.ru, you can map it to the “/MWRAP/” endpoint. As with the rest of the Mwrap API, your Rack server needs to be spawned with the mwrap(1) wrapper to enable the LD_PRELOAD.

require 'mwrap_rack'
map('/MWRAP') { run(MwrapRack.new) }
map('/') { run(your_normal_app) }

A live demo is available at 80x24.org/MWRAP/ (warning the demo machine is 32-bit, so counters will overflow)

This module is only available in mwrap 2.0.0+

Defined Under Namespace

Modules: HtmlResponse Classes: Each, EachAt, HeapPages

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

The standard Rack application endpoint for MwrapRack



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/mwrap_rack.rb', line 147

def call(env)
  case env['PATH_INFO']
  when %r{\A/each/(\d+)\z}
    min = $1.to_i
    m = env['QUERY_STRING'].match(/\bsort=(\w+)/)
    Each.new(env['SCRIPT_NAME'], min, m ? m[1] : nil).response
  when %r{\A/at/(.*)\z}
    loc = -CGI.unescape($1)
    loc = Mwrap[loc] or return r404
    EachAt.new(loc).response
  when '/heap_pages'
    HeapPages.new.response
  when '/'
    n = 2000
    u = 'https://80x24.org/mwrap/README.html'
    b = -('<html><head><title>Mwrap demo</title></head>' \
        "<body><p><a href=\"each/#{n}\">allocations &gt;#{n} bytes</a>" \
        "<p><a href=\"#{u}\">#{u}</a>" \
        "<p><a href=\"heap_pages\">heap pages</a>" \
        "</body></html>\n")
    [ 200, {'Content-Type'=>'text/html','Content-Length'=>-b.size.to_s},[b]]
  else
    r404
  end
end

#r404Object

:nodoc:



142
143
144
# File 'lib/mwrap_rack.rb', line 142

def r404 # :nodoc:
  [404,{'Content-Type'=>'text/plain'},["Not found\n"]]
end