// Respond is used to send a response to the user query
486502
func (q *Query) Respond(buf []byte) error {
	// Create response
	resp := q.createResponse(buf)

	// Encode response
	raw, err := encodeMessage(messageQueryResponseType, resp)
	if err != nil {
		return fmt.Errorf("failed to format response: %v", err)
	}

	if err := q.respondWithMessageAndResponse(raw, resp); err != nil {
		return fmt.Errorf("failed to respond to key query: %v", err)
	}

	return nil
}
430050
func sendResponse(w http.ResponseWriter, response interface{}, status int) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	marshalledResponse, err := json.Marshal(response)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(fmt.Sprintf("Failed to marshal response: %v %v", response, err)))
		return
	}
	w.WriteHeader(status)
	w.Write(marshalledResponse)
}
1003342
func Respond(pattern string, fn func(res *Response) error) handler {
	return &Handler{Method: RESPOND, Pattern: pattern, Run: fn}
}
749935
func (r *LookupRequest) Respond(resp *LookupResponse) {
	out := &entryOut{
		outHeader:      outHeader{Unique: uint64(r.ID)},
		Nodeid:         uint64(resp.Node),
		Generation:     resp.Generation,
		EntryValid:     uint64(resp.EntryValid / time.Second),
		EntryValidNsec: uint32(resp.EntryValid % time.Second / time.Nanosecond),
		AttrValid:      uint64(resp.AttrValid / time.Second),
		AttrValidNsec:  uint32(resp.AttrValid % time.Second / time.Nanosecond),
		Attr:           resp.Attr.attr(),
	}
	r.Conn.respond(&out.outHeader, unsafe.Sizeof(*out))
}
1258163
func (rep *Responder) Respond(request *http.Request) {
	// get request format from url
	if ext := filepath.Ext(request.URL.Path); ext != "" {
		if respond, ok := rep.responds[strings.TrimPrefix(ext, ".")]; ok {
			respond()
			return
		}
	}

	// get request format from Accept
	for _, accept := range strings.Split(request.Header.Get("Accept"), ",") {
		if exts, err := mime.ExtensionsByType(accept); err == nil {
			for _, ext := range exts {
				if respond, ok := rep.responds[strings.TrimPrefix(ext, ".")]; ok {
					respond()
					return
				}
			}
		}
	}

	// use first format as default
	if rep.DefaultResponder != nil {
		rep.DefaultResponder()
	}
	return
}
260012
func Respond(w http.ResponseWriter, statusCode int, response interface{}) {
	if response == nil {
		response = &MessageResponse{}
	} else if r, ok := response.(MessageResponse); ok {
		response = &r
	}
	if r, ok := response.(*MessageResponse); ok {
		if r.Code == 0 {
			r.Code = statusCode
		}
		if r.Message == "" {
			r.Message = http.StatusText(statusCode)
		}
	}
	var b bytes.Buffer
	enc := json.NewEncoder(&b)
	enc.SetEscapeHTML(EscapeHTML)
	if err := enc.Encode(response); err != nil {
		panic(err)
	}
	if DefaultContentTypeHeader != "" {
		w.Header().Set("Content-Type", DefaultContentTypeHeader)
	}
	w.WriteHeader(statusCode)
	fmt.Fprintln(w, b.String())
}
966692
func Responder(req *http.Request, mock *Response, res *http.Response) (*http.Response, error) {
	// If error present, reply it
	err := mock.Error
	if err != nil {
		return nil, err
	}

	if res == nil {
		res = createResponse(req)
	}

	// Apply response filter
	for _, filter := range mock.Filters {
		if !filter(res) {
			return res, nil
		}
	}

	// Define mock status code
	if mock.StatusCode != 0 {
		res.Status = strconv.Itoa(mock.StatusCode) + " " + http.StatusText(mock.StatusCode)
		res.StatusCode = mock.StatusCode
	}

	// Define headers by merging fields
	res.Header = mergeHeaders(res, mock)

	// Define mock body, if present
	if len(mock.BodyBuffer) > 0 {
		res.ContentLength = int64(len(mock.BodyBuffer))
		res.Body = createReadCloser(mock.BodyBuffer)
	}

	// Apply response mappers
	for _, mapper := range mock.Mappers {
		if tres := mapper(res); tres != nil {
			res = tres
		}
	}

	// Sleep to simulate delay, if necessary
	if mock.ResponseDelay > 0 {
		time.Sleep(mock.ResponseDelay)
	}

	return res, err
}
404296
func (t *SimpleChaincode) respond(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	if len(args) != 3 {
		return shim.Error("expected three arguments")
	}

	status, err := strconv.ParseInt(args[0], 10, 32)
	if err != nil {
		return shim.Error(err.Error())
	}
	message := args[1]
	payload := []byte(args[2])

	return pb.Response{
		Status:  int32(status),
		Message: message,
		Payload: payload,
	}
}
147694
func (r *CreateRequest) Respond(resp *CreateResponse) {
	eSize := entryOutSize(r.Header.Conn.proto)
	buf := newBuffer(eSize + unsafe.Sizeof(openOut{}))

	e := (*entryOut)(buf.alloc(eSize))
	e.Nodeid = uint64(resp.Node)
	e.Generation = resp.Generation
	e.EntryValid = uint64(resp.EntryValid / time.Second)
	e.EntryValidNsec = uint32(resp.EntryValid % time.Second / time.Nanosecond)
	e.AttrValid = uint64(resp.Attr.Valid / time.Second)
	e.AttrValidNsec = uint32(resp.Attr.Valid % time.Second / time.Nanosecond)
	resp.Attr.attr(&e.Attr, r.Header.Conn.proto)

	o := (*openOut)(buf.alloc(unsafe.Sizeof(openOut{})))
	o.Fh = uint64(resp.Handle)
	o.OpenFlags = uint32(resp.Flags)

	r.respond(buf)
}
1012560
func (c *Conn) RespondTo(to *protocol.Message, resp *protocol.Message) error {
	resp.ResponseTo = to.Id
	return c.Send(resp)
}
1352679
func (s *Server) sendResponse(resp *dns.Msg, from net.Addr, unicast bool) error {
	// TODO(reddaly): Respect the unicast argument, and allow sending responses
	// over multicast.
	buf, err := resp.Pack()
	if err != nil {
		return err
	}

	// Determine the socket to send from
	addr := from.(*net.UDPAddr)
	if addr.IP.To4() != nil {
		_, err = s.ipv4List.WriteToUDP(buf, addr)
		return err
	} else {
		_, err = s.ipv6List.WriteToUDP(buf, addr)
		return err
	}
}
98066
func (auth *PlainAuth) Response() string {
	return fmt.Sprintf("\000%s\000%s", auth.Username, auth.Password)
}
1370017
func (c pin) respond(err error) {
	select {
	case <-c.stop:
	case c.response <- err:
	}
}
1115537
func (a *adapter) Reply(res *hal.Response, strings ...string) error {
	newStrings := make([]string, len(strings))
	for _, str := range strings {
		newStrings = append(newStrings, fmt.Sprintf("%s: %s", res.UserName(), str))
	}

	a.Send(res, newStrings...)

	return nil
}
510717
func (packet *packetMock) Respond(msg *proto.GossipMessage) {
	sMsg, _ := msg.NoopSign()
	packet.src.socket <- &packetMock{
		src: packet.dst,
		dst: packet.src,
		msg: sMsg,
	}
}
803271
func SendResponse(w http.ResponseWriter, resp []*Response, batch bool) error {
	w.Header().Set(contentTypeKey, contentTypeValue)
	if batch || len(resp) > 1 {
		return fastjson.NewEncoder(w).Encode(resp)
	} else if len(resp) == 1 {
		return fastjson.NewEncoder(w).Encode(resp[0])
	}
	return nil
}
128352
func Reply(w http.ResponseWriter, response interface{}, status int) {
	// marshal the body of the response
	marshalledResponse, err := json.Marshal(response)
	if err != nil {
		marshalledResponse = []byte(fmt.Sprintf(`{"message": "Failed to marshal response: %v %v"}`, response, err))
		status = http.StatusInternalServerError
		LogRequest(nil, status, time.Nanosecond, err)
	}

	// write JSON response
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.WriteHeader(status)
	w.Write(marshalledResponse)
}