HTTP Plugin

Tutorial HTTP Plugin

A new awesome feature comes with Warp 10 2.0: HTTPPlugin!


It allows to expose an HTTP endpoint in Warp 10™ and handle requests with WarpScript™.


Presentation

The main idea is to expose an HTTP endpoint in Warp 10 which handle requests with a WarpScript.

It handles any kind of HTTP request and put a “request” structure on the top of the stack which contains many useful informations like the method, the path, the parameters and so on.

Configuration

First, you have to configure and activate the plugin in configuration file <path_to_warp10>/etc/conf-standalone.conf. Add those lines to your configuration file:

# Fully qualified classname
warp10.plugin.http=io.warp10.plugins.http.HTTPWarp10Plugin

# IP the HTTP plugin will listen on
http.host = ${standalone.host}

# Port the HTTP plugin will listen on
http.port = 9000

# Number of threads Jetty should use
http.maxthreads = 16

# Directory where spec files are located
http.dir = ${standalone.home}/http/

# Period (in ms) at which to scan 'http.dir' (defaults to 60s)
http.period = 50000

Restart Warp 10 in order to apply your changes.

In your Warp 10™ installation, create a folder called http, ie: <path_to_warp10>/http

First WarpScript™

At first, begin with a simple example. Create a new file call <path_to_warp10>/http/test.mc2 and add the content below:

{
  'path' '/test' // root path
  'prefix' true  // allow to get path info
  'parsePayload' true // parse payload of a POST url encoded
                 // false: to parse manually the payload request
  'macro' <%    
    'request' STORE
    {
      'status' 200  
      'body' $request ->JSON
      'headers' { 'Content-Type' 'application/json' }
    }
  %>
}

Used WarpScript functions : STORE, map creation

Then, in a browser, navigate to http://localhost:9000/test/a/b/c?d=1&e=2&f=3&f=4

It will return a JSON string containing your request information. You should have something like that:

{
  "headers":{
    "Accept":[
      "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
    ],
    "Upgrade-Insecure-Requests":[
      "1"
    ],
    "Connection":[
      "keep-alive"
    ],
    "User-Agent":[
      "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
    ],
    "Host":[
      "localhost:9000"
    ],
    "Accept-Encoding":[
      "gzip, deflate, br"
    ],
    "Accept-Language":[
      "fr,de-LI;q=0.9,de;q=0.8,pt;q=0.7,en-US;q=0.6,en;q=0.5"
    ]
  },
  "method":"GET",
  "params":{
    "d":[
      "1"
    ],
    "e":[
      "2"
    ],
    "f":[
      "3",
      "4"
    ]
  },
  "pathinfo":"/a/b/c",
  "target":"/test/a/b/c"
}

Hello World

Now we can create a bit more complicated script in order to build an “Hello World” HTML page generator. Create a new file call <path_to_warp10>/http/simple.hello.mc2 and add the content below:

{
  // root path
  'path' '/hello'
  // allow to get path info
  'prefix' true
  // true: parse payload of a POST url encoded request
  // false: to parse manually the payload
  'parsePayload' true   
  'macro' <%
    // actual executed code

    // save the original request into a variable
    'request' STORE 
    // retrieve the path info (after /hello)
    $request 'pathinfo' GET 
    // extract each parts of the path info
    '/' SPLIT 'res' STORE 
    // evince the first '/'
    $res [ 1 $res SIZE ] SUBLIST 'res' STORE
    // define an HTML template
    <'
<html>
  <head>
    <title>Hello</title>
  </head>
  <body>
    <h1>Hello {{name}}!</h1>
  </body>
</html>
    '> 
    // interpolate template's vars
    { 'name' $res 0 GET } TEMPLATE 
    // save the result into a variable
    'body' STORE
    // build the HTTP response
    { 
      'status' 200  
      'body' $body
      'headers' { 'Content-Type' 'text/html' }
    }
  %>
}

Used WarpScript functions : STORE, TEMPLATESUBLIST, SIZE, map creation

Then navigate to http://localhost:9000/hello/world

Going further

With this new feature it is now possible to have easy access to Warp 10 for any device. Show graphs, show results refreshed periodically. It’s up to you!

Share