Ejemplo n.º 1
0
func (h Handler) printResponse(status int, header map[string]string, content []byte) {
	headerEncoded := encodeData(header)

	h.response.WriteHeader(200)
	h.response.Header().Set("Content-Type", "image/gif")

	compressed := false
	if contentType, ok := header["content-type"]; ok {
		if strings.HasPrefix(contentType, "text/") || strings.HasPrefix(contentType, "application/json") || strings.HasPrefix(contentType, "application/javascript") {
			compressed = true
		}
	}

	if compressed {
		h.response.Write([]byte("1"))
		w, err := zlib.NewWriterDict(h.response, zlib.BestCompression, nil)
		if err != nil {
			h.context.Criticalf("zlib.NewWriterDict(h.response, zlib.BestCompression, nil) Error: %v", err)
			return
		}
		defer w.Close()
		binary.Write(w, binary.BigEndian, uint32(status))
		binary.Write(w, binary.BigEndian, uint32(len(headerEncoded)))
		binary.Write(w, binary.BigEndian, uint32(len(content)))
		w.Write(headerEncoded)
		w.Write(content)
	} else {
		h.response.Write([]byte("0"))
		binary.Write(h.response, binary.BigEndian, uint32(status))
		binary.Write(h.response, binary.BigEndian, uint32(len(headerEncoded)))
		binary.Write(h.response, binary.BigEndian, uint32(len(content)))
		h.response.Write(headerEncoded)
		h.response.Write(content)
	}
}
Ejemplo n.º 2
0
// NewFramer allocates a new Framer for a given SPDY connection, repesented by
// a io.Writer and io.Reader. Note that Framer will read and write individual fields
// from/to the Reader and Writer, so the caller should pass in an appropriately
// buffered implementation to optimize performance.
func NewFramer(w io.Writer, r io.Reader) (*Framer, os.Error) {
	compressBuf := new(bytes.Buffer)
	compressor, err := zlib.NewWriterDict(compressBuf, zlib.BestCompression, []byte(HeaderDictionary))
	if err != nil {
		return nil, err
	}
	framer := &Framer{
		w:                w,
		headerBuf:        compressBuf,
		headerCompressor: compressor,
		r:                r,
	}
	return framer, nil
}
Ejemplo n.º 3
0
// NewHeaderWriter creates a HeaderWriter ready to compress headers.
func NewHeaderWriter(level int) (hw *HeaderWriter) {
	hw = &HeaderWriter{buffer: new(bytes.Buffer)}
	hw.compressor, _ = zlib.NewWriterDict(hw.buffer, level, []byte(headerDictionary))
	return
}