[WIP] BHTTPServer - simple http server

Discussion in 'WIP and Development Status' started by DPOH-VAR, Feb 22, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    DPOH-VAR

    BHTTPServer is a small HTTP server written in groovy
    simple solution for the webserver with using AJAX

    Requirements:
    • VarScript 0.5.7 or latest o.5.x
    • Java 7 or Java 8 with option -noverify
    Installation:
    • Install VarScript
    • Install Groovy:
      • Create ./lib directory in the server root
      • Download Groovy (binary zip)
      • Find groovy-all-*-indy.jar and put it to ./lib folder
    • Start server
    • Wait for message:
      [VarScript] load Groovy Scripting Engine 2.0
      lang: Groovy 2.2.0-rc-3
      name: groovy,Groovy
      extension: groovy
    • Stop server
    • Install module BHTTPServer:
      • Download BHTTPServer.groovy
      • Put it to ./plugins/VarScript/scripts/Groovy/modules/
      • Open file with text editor and change config (in the end of file):
    • Start server and wait for message:
      [VarScript] [Groovy] <#> module BHTTPServer loaded.
    • Create file index.htm in static folder
    • Check it: http://localhost:8011
    How to use:

    pathStatic - home directory for your static content.
    Put here html files, js, etc.

    pathExecute - home directory for scripts.
    you can write script on groovy, javascript or or in any other language, compatible with VarScript
    request http://localhost:8011/login tries to open ./webserver/execute/login.js or ./webserver/execute/login.groovy

    available variables in scripts:
    method - string "GET", "POST", "OPTIONS" etc
    query - map <string, string> with query
    cookie - map <string, string> with cookies
    session - map <string,?> session container
    exchange - interface for sending response
    Also Bukkit API is available.
    You can add missing jars and classes to folder ./lib
    Code:javascript
    1. // add header to response
    2. exchange.header("Expires","Tue, 31 Jan 2012 15:02:53 GMT")
    3. // set response status
    4. exchange.setStatus(200)
    5. // send status and headers
    6. exchange.sendHeaders()
    7. // send text
    8. exchange.send("text")
    9. // send byte array
    10. exchange.send( new File("filename.txt") )
    11. // parse object as JSON string and send to client
    12. exchange.sendJson( object )
    13. // set cookie for client
    14. exchange.setCookie("key", "val", )
    15. // set cookie with expires date
    16. exchange.setCookie("key", "val", new Date() )
    17. // close connection
    18. exchange.close()
    19. // include other script file, return result
    20. def result = exchange.include("filename.js")
    21. // get JSON object from request body
    22. def json = excnange.getJson()
    23. // get JSON object from string
    24. def json = excnange.getJson('{"foo":"bar"}')

    if script returns string or file - it will be sended to client
    if script returns true - connection will not be closed automatically
    you can use these aliases with groovy:
    Code:groovy
    1. include "filename.groovy" // include file
    2. setStatus 404 // set response status
    3. header "Content-Type", "text/html" // add header to response
    4. sendJson([a:1, b:2]) // send object as JSON string
    5. send "text" // send text
    6. send new File("filename") // send file
    7. setCookie "key", "value", new Date() + 10 // cookie for 10 days
    8. getJson() // get JSON object from request body
    9.  

    Example: login.groovy: Check login and password with plugin AuthMe
    Code:groovy
    1. import fr.xephi.authme.api.API
    2.  
    3. if (method != "POST") return "POST only"
    4.  
    5. switch (query.action) {
    6. case "logout":
    7. session.player = null
    8.  
    9. case "check":
    10. return asJson([player: session.player])
    11.  
    12. case "login":
    13. def auth = API.checkPassword query.login, query.password
    14. if (auth) session.player = query.login
    15. else session.player = null
    16. return asJson([result: auth, player: session.player])
    17. }

    Download example (.html files and .groovy scripts)
    screenshot (open)
    exm.png
     
Thread Status:
Not open for further replies.

Share This Page