Пример #1
0
// writeHeaders writes the given headers using the given function to get the
// arg writer.
//
// This may be used with the Arg2Writer functions on OutboundCall and
// InboundCallResponse.
//
// If the format is JSON, the headers are JSON encoded.
func writeHeaders(format tchannel.Format, headers transport.Headers, getWriter func() (tchannel.ArgWriter, error)) error {
	if format == tchannel.JSON {
		// JSON is special
		return tchannel.NewArgWriter(getWriter()).WriteJSON(headers.Items())
	}
	return tchannel.NewArgWriter(getWriter()).Write(encodeHeaders(headers))
}
Пример #2
0
func (rw *responseWriter) AddHeaders(h transport.Headers) {
	if rw.wroteHeaders {
		panic("AddHeaders() cannot be called after calling Write().")
	}
	for k, v := range h.Items() {
		rw.headers = rw.headers.With(k, v)
	}
}
Пример #3
0
// toHTTPHeaders converts application headers into transport headers.
//
// Headers are read from 'from' and written to 'to'. The final header collection
// is returned.
//
// If 'to' is nil, a new map will be assigned.
func (hm headerMapper) ToHTTPHeaders(from transport.Headers, to http.Header) http.Header {
	if to == nil {
		to = make(http.Header, from.Len())
	}
	for k, v := range from.Items() {
		to.Add(hm.Prefix+k, v)
	}
	return to
}
Пример #4
0
// fromHTTPHeaders converts HTTP headers to application headers.
//
// Headers are read from 'from' and written to 'to'. The final header collection
// is returned.
//
// If 'to' is nil, a new map will be assigned.
func (hm headerMapper) FromHTTPHeaders(from http.Header, to transport.Headers) transport.Headers {
	prefixLen := len(hm.Prefix)
	for k := range from {
		if strings.HasPrefix(k, hm.Prefix) {
			key := k[prefixLen:]
			to = to.With(key, from.Get(k))
		}
		// Note: undefined behavior for multiple occurrences of the same header
	}
	return to
}
Пример #5
0
// checkSuperSet checks if the items in l are all also present in r.
func checkSuperSet(l, r transport.Headers) error {
	missing := make([]string, 0, l.Len())
	for k, vl := range l.Items() {
		vr, ok := r.Get(k)
		if !ok || vr != vl {
			missing = append(missing, k)
		}
	}

	if len(missing) > 0 {
		return fmt.Errorf("missing headers: %v", strings.Join(missing, ", "))
	}
	return nil
}
Пример #6
0
// encodeHeaders encodes headers using the format:
//
// 	nh:2 (k~2 v~2){nh}
func encodeHeaders(hs transport.Headers) []byte {
	if hs.Len() == 0 {
		return []byte{0, 0} // nh = 2
	}

	size := 2 // nh:2
	for k, v := range hs.Items() {
		size += len(k) + 2 // k~2
		size += len(v) + 2 // v~2
	}

	out := make([]byte, size)

	i := 2
	binary.BigEndian.PutUint16(out, uint16(hs.Len())) // nh:2
	for k, v := range hs.Items() {
		i += _putStr16(k, out[i:]) // k~2
		i += _putStr16(v, out[i:]) // v~2
	}

	return out
}
Пример #7
0
// AddHeaders for FakeResponseWriter.
func (fw *FakeResponseWriter) AddHeaders(h transport.Headers) {
	for k, v := range h.Items() {
		fw.Headers = fw.Headers.With(k, v)
	}
}