Friday, February 28, 2014

How to install Lighttpd on Mountain Lion OS X 10.8

Below are few steps you need to follow to install Lighttpd on 1.4.34 on Mac OS X 10.8 (Mountain Lion)

1) Download Lighttpd from http://www.lighttpd.net/

2) If you don't have command line tools installed (by default they are not) then download it from


These tools include C compiler which is required to make lighttpd.

3) Now download and install pcre library  pcre-8.34.tar.bz2
http://sourceforge.net/projects/pcre/files/latest/download?source=files
Its one of the dependency required by lighttpd

tar xzf pcre-7.9.tar.gz
cd pcre-7.9
./configure
make && sudo make install
cd ../

4) Now install lighttpd

tar xzf lighttpd-*.tar.gz
cd lighttpd-*
./configure
make && sudo make install

While installation, terminal might show below messages
make[5]: Nothing to be done for `all'.
make[5]: Nothing to be done for `all-am'.
make[4]: Nothing to be done for `all-am'.
make[3]: Nothing to be done for `all-am'.

These are harmless messages and don't get puzzled by them.

5) Now add /usr/local/sbin to PATH environment variable

6) Then create lighttpd.conf file with below content

server.document-root = "/var/www/servers/www.example.org/pages/" 

server.port = 3000

mimetype.assign = (
  ".html" => "text/html", 
  ".txt" => "text/plain",
  ".jpg" => "image/jpeg",
  ".png" => "image/png" 
)

7) First, check that your configuration is ok:

$ lighttpd -t -f lighttpd.conf
8) Now start the server for testing:

$ lighttpd -D -f lighttpd.conf
and point your browser to http://127.0.0.1:3000/

Browser might show "Not Found" because server.document-root might not be correct in your case.

9) To stop the server, return to the command prompt and press ctrl-c.

Thursday, May 2, 2013

Play Framework 2.1 CSRF Filter Java Example

I have been searching for CSRF filter examples for Play framework 2.1 (Java) but couldn't find any
so I tried myself. Got it working so sharing the code.

----------------------------------------------
app/controllers/Application.java
----------------------------------------------

package controllers;

import static play.data.Form.form;

import java.util.HashMap;
import java.util.Map;

import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.testform;

public class Application extends Controller {

public static Result index() {
Form<User> userForm = form(User.class);
Map<String, String> anyData = new HashMap();
anyData.put("email", "bob@gmail.com");
anyData.put("password", "secret");
User user = userForm.bind(anyData).get();
return ok(testform.render(userForm.fill(user)));
}

public static Result formAction() {
return ok("Headers:"+ctx().request().headers()+" Query String:"+ctx().request().queryString());
}
}
----------------------------------------------
app/Global.java
----------------------------------------------

import play.*;
import play.api.mvc.EssentialFilter;
import play.filters.csrf.CSRFFilter;

public class Global extends GlobalSettings {
@Override
 public void onStart(Application app) {
   Logger.info("********************Application has started");
 }

 @Override
 public void onStop(Application app) {
   Logger.info("********************Application shutdown...");
 }

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public <T extends EssentialFilter> Class<T>[] filters() {
Class[] filters = {CSRFFilter.class};
return filters;
}
}

----------------------------------------------
app/views/testform.scala.html
----------------------------------------------

@(signupForm: Form[User])
@import helper._
@main("FORM Example") {
    @helper.form(action = helper.CSRF(routes.Application.formAction)) {
        <fieldset>
            <legend>Account informations</legend>
         
         
            @inputText(
                signupForm("email"), '_label -> "Email",
                '_help -> "Enter a valid email address."
            )
         
            @inputText(
                signupForm("password"),
                '_label -> "Password",
                '_help -> "Please specify password.",
                '_error -> signupForm.globalError
            )
        </fieldset>
        <div class="actions">
            <input type="submit" class="btn primary" value="Sign Up">
        </div>
    }
 }

----------------------------------------------
project/Build.scala
----------------------------------------------

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

  val appName         = "cxrf"
  val appVersion      = "1.0-SNAPSHOT"

  val appDependencies = Seq(
    // Add your project dependencies here,
    javaCore,
    javaJdbc,
    javaEbean,
    filters
  )
  val main = play.Project(appName, appVersion, appDependencies).settings(
    // Add your own project settings here    
  )
}
----------------------------------------------