Entry 5099

HTTP Server (v1)

   

Submitted by anonymous on July 10, 2010 at 10:35 p.m.
Language: Go. Code size: 1.5 KB.

package main

import (
    "http"
    "regexp"
    //"json"
    "math"
    //mongo "github.com/mikejs/gomongo/mongo"
)



func hex2dec(hex string) uint64 {

    translator := map[byte]int{'0':0, '1':1 , '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'a':10, 'b':11, 'c':12, 'd':13, 'e':14, 'f': 15}
    dec := uint64(0)
    length := len(hex)

    for i, v := range hex {

        dec += uint64(float64(translator[uint8(v)]) * math.Pow(float64(16), float64(length - i - 1)))

    }

    return dec
}

// Streams the data.
func Stream(c *http.Conn, r *http.Request) {

    R := *r

    // /<id>
    reg, err := regexp.Compile("^/([0-9a-f]+)$")

    if err != nil {

        (*c).WriteHeader(500)
        return

    } else if reg.MatchString(R.URL.Path) {

        //query := make(map[string]mongo.BSON)
        (*c).WriteHeader(200)
        s := "Hi there!"
        (*c).SetHeader("Content-Length", "9")
        (*c).Write([]byte(s))
        return
    }

    // /<id>/since:<time>
    reg, err = regexp.Compile("^/([0-9a-f]+)/since:([0-9a-f]+)$")

    if err != nil {

        (*c).WriteHeader(500)
        return

    } else if reg.MatchString(R.URL.Path) {

        // Write the stoliczek object's modifications since the specified
        // unix time.
        (*c).WriteHeader(200)
        (*c).Flush()
        return
    }

    http.NotFound(c, r)
}

func main() {

    http.ListenAndServe(":8800", http.HandlerFunc(Stream))

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).