diff options
author | Vidhu Kant Sharma <bokuwakanojogahoshii@yahoo.com> | 2020-10-19 11:05:36 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <bokuwakanojogahoshii@yahoo.com> | 2020-10-19 11:05:36 +0530 |
commit | 9c2ad91230d72fe6d661450cc78300ea223ae2bc (patch) | |
tree | 17bd774b3f972359dfbb46248f085bbf1c6dae98 /vendor/github.com/manucorporat |
make it better
Diffstat (limited to 'vendor/github.com/manucorporat')
-rw-r--r-- | vendor/github.com/manucorporat/sse/.travis.yml | 6 | ||||
-rw-r--r-- | vendor/github.com/manucorporat/sse/LICENSE | 21 | ||||
-rw-r--r-- | vendor/github.com/manucorporat/sse/README.md | 54 | ||||
-rw-r--r-- | vendor/github.com/manucorporat/sse/sse-encoder.go | 106 | ||||
-rw-r--r-- | vendor/github.com/manucorporat/sse/writer.go | 24 |
5 files changed, 211 insertions, 0 deletions
diff --git a/vendor/github.com/manucorporat/sse/.travis.yml b/vendor/github.com/manucorporat/sse/.travis.yml new file mode 100644 index 0000000..3d33833 --- /dev/null +++ b/vendor/github.com/manucorporat/sse/.travis.yml @@ -0,0 +1,6 @@ +language: go +sudo: false +go: + - 1.3 + - 1.4 + - tip diff --git a/vendor/github.com/manucorporat/sse/LICENSE b/vendor/github.com/manucorporat/sse/LICENSE new file mode 100644 index 0000000..1ff7f37 --- /dev/null +++ b/vendor/github.com/manucorporat/sse/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Manuel MartÃnez-Almeida + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/vendor/github.com/manucorporat/sse/README.md b/vendor/github.com/manucorporat/sse/README.md new file mode 100644 index 0000000..4e1cf0e --- /dev/null +++ b/vendor/github.com/manucorporat/sse/README.md @@ -0,0 +1,54 @@ +#Server-Sent Events [![GoDoc](https://godoc.org/github.com/manucorporat/sse?status.svg)](https://godoc.org/github.com/manucorporat/sse) [![Build Status](https://travis-ci.org/manucorporat/sse.svg)](https://travis-ci.org/manucorporat/sse) + +Server-sent events (SSE) is a technology where a browser receives automatic updates from a server via HTTP connection. The Server-Sent Events EventSource API is [standardized as part of HTML5[1] by the W3C](http://www.w3.org/TR/2009/WD-eventsource-20091029/). + +- [Real world demostration using Gin](http://sse.getgin.io/) +- [Read this great SSE introduction by the HTML5Rocks guys](http://www.html5rocks.com/en/tutorials/eventsource/basics/) +- [Browser support](http://caniuse.com/#feat=eventsource) + +##Sample code + +```go +import "github.com/manucorporat/sse" + +func httpHandler(w http.ResponseWriter, req *http.Request) { + // data can be a primitive like a string, an integer or a float + sse.Encode(w, sse.Event{ + Event: "message", + Data: "some data\nmore data", + }) + + // also a complex type, like a map, a struct or a slice + sse.Encode(w, sse.Event{ + Id: "124", + Event: "message", + Data: map[string]interface{}{ + "user": "manu", + "date": time.Now().Unix(), + "content": "hi!", + }, + }) +} +``` +``` +event: message +data: some data\\nmore data + +id: 124 +event: message +data: {"content":"hi!","date":1431540810,"user":"manu"} + +``` + +##Content-Type + +```go +fmt.Println(sse.ContentType) +``` +``` +text/event-stream +``` + +##Decoding support + +There is a client-side implementation of SSE coming soon.
\ No newline at end of file diff --git a/vendor/github.com/manucorporat/sse/sse-encoder.go b/vendor/github.com/manucorporat/sse/sse-encoder.go new file mode 100644 index 0000000..16bab76 --- /dev/null +++ b/vendor/github.com/manucorporat/sse/sse-encoder.go @@ -0,0 +1,106 @@ +// Copyright 2014 Manu Martinez-Almeida. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sse + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "reflect" + "strconv" + "strings" +) + +// Server-Sent Events +// W3C Working Draft 29 October 2009 +// http://www.w3.org/TR/2009/WD-eventsource-20091029/ + +const ContentType = "text/event-stream" + +var contentType = []string{ContentType} +var noCache = []string{"no-cache"} +var replacer = strings.NewReplacer("\n", "\\n", "\r", "\\r") + +type Event struct { + Event string + Id string + Retry uint + Data interface{} +} + +func Encode(writer io.Writer, event Event) error { + w := checkWriter(writer) + writeId(w, event.Id) + writeEvent(w, event.Event) + writeRetry(w, event.Retry) + return writeData(w, event.Data) +} + +func writeId(w stringWriter, id string) { + if len(id) > 0 { + w.WriteString("id: ") + writeEscape(w, id) + w.WriteString("\n") + } +} + +func writeEvent(w stringWriter, event string) { + if len(event) > 0 { + w.WriteString("event: ") + writeEscape(w, event) + w.WriteString("\n") + } +} + +func writeRetry(w stringWriter, retry uint) { + if retry > 0 { + w.WriteString("retry: ") + w.WriteString(strconv.FormatUint(uint64(retry), 10)) + w.WriteString("\n") + } +} + +func writeData(w stringWriter, data interface{}) error { + w.WriteString("data: ") + switch kindOfData(data) { + case reflect.Struct, reflect.Slice, reflect.Map: + err := json.NewEncoder(w).Encode(data) + if err != nil { + return err + } + w.WriteString("\n") + default: + text := fmt.Sprint(data) + writeEscape(w, text) + w.WriteString("\n\n") + } + return nil +} + +func (r Event) Render(w http.ResponseWriter) error { + header := w.Header() + header["Content-Type"] = contentType + + if _, exist := header["Cache-Control"]; !exist { + header["Cache-Control"] = noCache + } + return Encode(w, r) +} + +func kindOfData(data interface{}) reflect.Kind { + value := reflect.ValueOf(data) + valueType := value.Kind() + if valueType == reflect.Ptr { + valueType = value.Elem().Kind() + } + return valueType +} + +func writeEscape(w stringWriter, str string) { + // any-char = %x0000-0009 / %x000B-000C / %x000E-10FFFF + // ; a Unicode character other than U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR) + replacer.WriteString(w, str) +} diff --git a/vendor/github.com/manucorporat/sse/writer.go b/vendor/github.com/manucorporat/sse/writer.go new file mode 100644 index 0000000..6f9806c --- /dev/null +++ b/vendor/github.com/manucorporat/sse/writer.go @@ -0,0 +1,24 @@ +package sse + +import "io" + +type stringWriter interface { + io.Writer + WriteString(string) (int, error) +} + +type stringWrapper struct { + io.Writer +} + +func (w stringWrapper) WriteString(str string) (int, error) { + return w.Writer.Write([]byte(str)) +} + +func checkWriter(writer io.Writer) stringWriter { + if w, ok := writer.(stringWriter); ok { + return w + } else { + return stringWrapper{writer} + } +} |