Programming

Toy Scala Static Webserver in less than 100 lines

Doh! The original link to the code was for the wrong file! Sorry. Corrected below.

Okay so there has been a craze for writing static webservers in new languages in less than 100 lines recently and I am not claiming that this code is anything special but I wanted to give to give the NetBeans Scala plugin a go (in NetBeans 6.5) so here’s my version of a Scala static webserver (in less than 100 lines, natch).

The code is more of a Toy at the moment as it assumes a very happy path. However it does work and it did provide some useful learning about Scala.

The good stuff includes: compact code, class imports, XML literals, map literal syntax and the NetBeans plugin does a good job of providing codealong feedback from the compiler.

Confusing stuff:

  • accessing array indexes with () not [], it makes sense but you have to get used to when coming from Java
  • implementing Java interfaces in Scala: still not totally sure how you do that
  • getting the right type to allow a Java API to be called: Array[Byte] seemed to take a long time to get right and not having type-coercion for Scala Lists to Java Lists means there is a anemic list variable in play
  • functions that have many parameters make for confusing IDE errors; do you want Int, Int, Int, Int, String, String or Int, Int, Int, Int?

And finally the bad:

  • the streaming IO for binary files is entirely imperative and is basically Java code, I’ve been told that Scalax can help put a prettier front on that
  • nothing to do with Scala but the in-built Java HttpServer should have had the public API in interfaces and there should have had an NIO-based HttpExchange
  • I.cant.stop.using.periods even.if.I.dont.have.to

Overall I am pretty happy with the quality of the final code and I feel I’m finding the balance of the language more and seeing Scala as more of an extension of Java than something entirely unique.

Standard

4 thoughts on “Toy Scala Static Webserver in less than 100 lines

  1. Alexey says:

    You your code don’t show any Scalish stuff 😦 Just replace vals with types and defs with public void and you get your old plain Java.

  2. Well it didn’t help linking to the wrong code. But yes you’re right what we have here is just a concise little Java program.

    I guess what interested me in doing was: a) Scala stuff is Java stuff to some degree, it’s the same libraries at the end of the day; b) the problem didn’t lend itself to being solved in a Scala unique way.

    In some ways Splitting the Loot allowed me to use more unique features in it.

  3. Nick says:

    Thanks for the code, rather useful.

    One little bug (which is actually quite bad and a bit tricky to track down):

    This code:
    while((input.read(buffer)) != -1) {
    output.write(buffer);
    }

    is wrong. Unless the file you’re sending is exactly a multiple of the size of the buffer, you’ll end up sending out some random bytes from the middle of the file at the end because the last input.read won’t completely fill the buffer, but there will still be leftover bytes from the previous read.

    To fix it, change to:
    var r = input.read(buffer)
    while (r != -1) {
    output.write(buffer,0,r)
    r = input.read(buffer)
    }

    • Thanks for the correction, it’s been a while since I looked at this code. I’ll try and update it to a more modern Scala version and perhaps NIO file-reading since I don’t really want to read the file piece by piece.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s