masto_util.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package main import ( "crypto/rand" "fmt" "net/http" "time" "humungus.tedunangst.com/r/webs/junk" ) type NotResponseWriter struct { auth string header http.Header status int } func (w *NotResponseWriter) Header() http.Header { return w.header } func (w *NotResponseWriter) Write(b []byte) (int, error) { dlog.Println("notresponsewriter: oauth code:", string(b)) w.auth = string(b) return 0, nil } func (w *NotResponseWriter) WriteHeader(statusCode int) { w.status = statusCode } func snowflake() uint64 { ts := time.Now() return uint64(uint64(ts.UnixNano()/int64(time.Millisecond))<<16 | uint64(time.Now().Nanosecond()&0xffff)) } func badjunk(rw http.ResponseWriter, j junk.Junk, path string) { elog.Printf("%s: bad junk: %s", path, j.ToString()) http.Error(rw, fmt.Sprintf("%s: bad junk", path), http.StatusBadRequest) } func goodjunk(rw http.ResponseWriter, j junk.Junk) { rw.Header().Set("Content-Type", "application/json; charset=utf-8") rw.WriteHeader(http.StatusOK) rw.Write(j.ToBytes()) } func tokengen() string { b := make([]byte, 32) rand.Read(b) return fmt.Sprintf("%x", b) } |