Golo - Little 5

Ok! That’s time to sync everyone on the state of GoloHttpServer.
First of all, I created a GitHub for this and you’re more than welcome to contribute, ask questions or even just talk about it.

This will allow you to take a look at the implementation of the run function which handle the Http request and response.

function run = |connectedClient, serverState| {       

  let inFromClient = BufferedReader( InputStreamReader (connectedClient:getInputStream()))
  let outToClient = DataOutputStream(connectedClient:getOutputStream())

  let requestString = inFromClient:readLine()
  let headerLine = requestString

  let tokenizer = StringTokenizer(headerLine)
  let httpMethod = tokenizer:nextToken()
  let httpQueryString = tokenizer:nextToken()
  let responseBuffer = StringBuffer()
  responseBuffer:append("<b> This is the HTTP Server Home Page.... </b><BR>")
    responseBuffer:append("The HTTP Client request is ....<BR>")

    while (inFromClient:ready()) {
        # Read the HTTP complete HTTP Query
        responseBuffer:append(requestString + "<BR>")
      requestString = inFromClient:readLine()
  }

  httpMethodHandle(httpMethod, httpQueryString, responseBuffer, outToClient)
}

As you can see, I lied a bit: the run function is just here to prepare all the needed element to delegate to httpMethodHandle.
The input and output buffers are initialized, the query is tokenized and everything is set to httpMethodHandle function.

function httpMethodHandle = |methodName, queryString, responseBuffer, outToClient| {
  case {
      when methodName == "GET" {
          var fileName = queryString:replaceFirst("/", "")
          if (queryString=="/") {
              # The default home page
              fileName = Parameters(): HOME()   
          }
          fileName = URLDecoder.decode(fileName)
          if ( File(fileName):isFile()){                              
              sendResponse(200, fileName, true, outToClient)
          } else {
              sendResponse(404, "<b>The Requested resource not found ...." + "Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/<fileName></b>", false, outToClient)
          }    
      }
      otherwise {
          sendResponse(404, "<b>The Requested resource not found ...." + "Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/<fileName></b>", false, outToClient)
          }
      }
}

This method has a simple goal: analyze the resource name asked and return the appropriate content.
Note the use of case..when..otherwise Golo statements.
This is quite useful isn’t it ?

From here, I’ll try to use GoloHttpServer for side projects.
For example, during MixIT 2013, I attended a conference on the Dart language.
Serve HTML pages and javascripts ? That may be doable with GoloHttpServer ;)

As usual, I’m waiting your emails and comments.
Don’t be shy and drop me an email as the other little hackers that prefers that to public comment! As of today, everyone had my answers ;)