Coder Social home page Coder Social logo

wai-servlet's Introduction

wai-servlet

CircleCI

Library to integrate eta wai applications with the servlet api

Getting Started

  • Visit the Getting Started eta instructions to build and install the library and the examples
  • In src/Network/Wai/Servlet/Examples.hs you can find some examples of wai applications and code to generate a servlet class that can be deployed in your favorite servlet container.
  • There are two options to deploy wai-servlet apps: generating a war file to be deployed in a servlet container or run it directly in a embedded one; this is the way more similar to use wai warp for haskell wai applications.

Running an application in a embedded servlet container

  • You need to install and set the wai-servlet-jetty-adapter package as a dependency. Currently is the unique adapter implemented for wai-servlet.
  • You have to import the module Network.Wai.Servlet.Handler.Jetty with the run function and call it with the port server and your application:
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Types                 (status200)
import Network.Wai
import Network.Wai.Servlet
import Network.Wai.Servlet.Handler.Jetty

appSimple :: Application
appSimple _ respond = respond $
   responseLBS status200 [("Content-Type", "text/plain")] "Hello World"

main = run 3000 appSimple

Generating a war to be deployed in a servlet container

  • This option supposes some manual steps. There is plans to make etlas build automatically a war file, tracked in this issue.
  • The main function to generate the servlet is Network.Wai.Servlet.makeServiceMethod for example:
{-# LANGUAGE OverloadedStrings #-}
import Java
import Network.Wai
import Network.HTTP.Types                 (status200)
import Network.Wai.Servlet

appSimple :: Application
appSimple _ respond = respond $
   responseLBS status200 [("Content-Type", "text/plain")] "Hello World"

servSimple :: DefaultWaiServletApplication
servSimple = makeServiceMethod appSimple

foreign export java "service" servSimple :: DefaultWaiServletApplication
  • This code will generate a network.wai.servlet.DefaultWaiServlet java class that extends javax.servlet.GenericServlet suitable to use in a standard war file
  • To generate the war you have to create the standard directory structure:
    • webApp
      • static resources (jsp,html,etc)
      • META-INF
      • WEB-INF
        • web.xml (required if you use servlets)
        • classes
        • lib
          • wai-servlet-app.jar
  • The web.xml for the default wai servlet could be
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name>wai-servlet-test</display-name>

	<servlet>
		<servlet-name>wai-servlet</servlet-name>
		<servlet-class>network.wai.servlet.DefaultWaiServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>wai-servlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>

wai-servlet's People

Contributors

jneira avatar rahulmutt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

rahulmutt

wai-servlet's Issues

Not in scope: ‘unsafePerformJavaWith’ when attempting to build during installation

Resolving dependencies...
Configuring wai-servlet-0.1.5.0...
Building wai-servlet-0.1.5.0...
Failed to install wai-servlet-0.1.5.0
Build log ( /home/funnyusername/.etlas/logs/eta-0.7.0.2/wai-servlet-0.1.5.0-I0egAxeJrMBLj7pkRuaDXn.log ):
etlas: Entering directory '/tmp/etlas-tmp-12590'
Configuring wai-servlet-0.1.5.0...
Preprocessing library for wai-servlet-0.1.5.0..
Building library for wai-servlet-0.1.5.0..
[1 of 7] Compiling Network.Wai.Servlet.File ( src/Network/Wai/Servlet/File.hs, dist/build/Network/Wai/Servlet/File.jar )
[2 of 7] Compiling Javax.Servlet    ( src/Javax/Servlet.hs, dist/build/Javax/Servlet.jar )
[3 of 7] Compiling Network.Wai.Servlet.Settings ( src/Network/Wai/Servlet/Settings.hs, dist/build/Network/Wai/Servlet/Settings.jar )
[4 of 7] Compiling Network.Wai.Servlet.Request ( src/Network/Wai/Servlet/Request.hs, dist/build/Network/Wai/Servlet/Request.jar )

src/Network/Wai/Servlet/Request.hs:80:21:
    Not in scope: ‘unsafePerformJavaWith’
etlas: Leaving directory '/tmp/etlas-tmp-12590'
etlas: Error: some packages failed to install:
wai-servlet-0.1.5.0-I0egAxeJrMBLj7pkRuaDXn failed during the building phase.
The exception was:
ExitFailure 1
wai-servlet-handler-jetty-0.1.2.0-6kNESzjPZ1EbRxix0NIil depends on
wai-servlet-handler-jetty-0.1.2.0 which failed to install.

This could be linked to typelead/eta#678, most likely unreleased
But I also do see

doJavaWith :: (Class c) => c -> Java c a -> a
#ifdef PURE_JAVA_WITH
doJavaWith = pureJavaWith
#else
doJavaWith = unsafePerformJavaWith
#endif

which might be in place for backward compatibility?

Enable logging for wai-servlet apps

Fix memory leak

There's a memory leak when allocating a foreign ptr to store the request body contents. We can get around this by using finalizerFree, but that currently triggers a bug with Eta v0.7.0b1, so I have kept it commented until we fix that bug.

Setup CircleCI

At minimum, there should be a build that runs a simple etlas build for basic verification.

Add gradle support

  • If possible we'll try to keep etlas installation
  • Using the eta plugin
  • Not sure if a special war goal could be added in this project

Make a 1.1.0.0 release for Eta v0.0.9

Can you make a wai-servlet-1.1.0.0 release? I just tested the servant example with Eta v0.0.9 and it's working pretty well with the changes I just made. If you can upload the new version, users can seamlessly try the example without any breakage. Thanks!

Incorrect handling of uris with non ascii chars

Actually, wai-servlet handle incorrectly (and differently than wai) uris containing non ascii chars. For example:

  • For /caña it gives us: rawPathInfo = "ca%F1a" and pathInfo = ["ca\65533"]
  • With Wai: rawPathInfo = "/ca%C3%B1a and pathInfo = ["ca\241a"]

httpVersion is partial

httpVersion ::  (a <: ServletRequest) => a -> H.HttpVersion
httpVersion req = pureJavaWith req $ do 
  httpVer <- getProtocol
  return $ case httpVer of
    "HTTP/0.9" -> H.http09
    "HTTP/1.0" -> H.http10
    "HTTP/1.1" -> H.http11

This is the current definition of httpVersion. Is there a way to smoothly handle the situation when a newer version of HTTP is used?

Implement the wai ResponseFile type handling in Network.Wai.Servlet.Response.updateHttpServletResponse

Incorrect handling of request body

  • The example app that shows the request body behaves incorrectly
  • It shows incorrect additional content after the actual body request
  • With the example app running doing curl http://localhost:3000 -method post -body "hola" i get:
    hola Content/Type text/plain" instead hola
  • It seems the handling of the body request servlet input stream with foreign pointers is not workinkg correctly

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.