Ejemplo n.º 1
0
func (h HTTPSHandler) agentHandler(handlerFunc boshhandler.Func) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			w.WriteHeader(404)
			return
		}

		rawJSONPayload, err := ioutil.ReadAll(r.Body)
		if err != nil {
			err = bosherr.WrapError(err, "Reading http body")
			h.logger.Error("https_handler", err.Error())
			return
		}

		respBytes, _, err := boshhandler.PerformHandlerWithJSON(
			rawJSONPayload,
			handlerFunc,
			boshhandler.UnlimitedResponseLength,
			h.logger,
		)
		if err != nil {
			err = bosherr.WrapError(err, "Running handler in a nice JSON sandwhich")
			h.logger.Error("https_handler", err.Error())
			return
		}

		_, err = w.Write(respBytes)
		if err != nil {
			err = bosherr.WrapError(err, "Writing response")
			h.logger.Error("https_handler", err.Error())
		}
	}
}
Ejemplo n.º 2
0
func (h *natsHandler) handleNatsMsg(natsMsg *yagnats.Message, handlerFunc boshhandler.Func) {
	respBytes, req, err := boshhandler.PerformHandlerWithJSON(
		natsMsg.Payload,
		handlerFunc,
		responseMaxLength,
		h.logger,
	)
	if err != nil {
		h.logger.Error(h.logTag, "Running handler: %s", err)
		return
	}

	if len(respBytes) > 0 {
		err = h.client.Publish(req.ReplyTo, respBytes)
		if err != nil {
			h.logger.Error(h.logTag, "Publishing to the client: %s", err.Error())
		}
	}
}
func (h HTTPSHandler) agentHandler(handlerFunc boshhandler.Func) (agentHandler func(http.ResponseWriter, *http.Request)) {
	agentHandler = func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			w.WriteHeader(404)
			return
		}

		if h.requestNotAuthorized(r) {
			w.Header().Add("WWW-Authenticate", `Basic realm=""`)
			w.WriteHeader(401)
			return
		}

		rawJSONPayload, err := ioutil.ReadAll(r.Body)
		if err != nil {
			err = bosherr.WrapError(err, "Reading http body")
			h.logger.Error("https_handler", err.Error())
			return
		}

		respBytes, _, err := boshhandler.PerformHandlerWithJSON(
			rawJSONPayload,
			handlerFunc,
			boshhandler.UnlimitedResponseLength,
			h.logger,
		)
		if err != nil {
			err = bosherr.WrapError(err, "Running handler in a nice JSON sandwhich")
			h.logger.Error("https_handler", err.Error())
			return
		}

		_, err = w.Write(respBytes)
		if err != nil {
			err = bosherr.WrapError(err, "Writing response")
			h.logger.Error("https_handler", err.Error())
		}
	}
	return
}