示例#1
0
func (handler *Characteristics) ServeHTTP(response http.ResponseWriter, request *http.Request) {
	var res io.Reader
	var err error

	handler.mutex.Lock()
	switch request.Method {
	case hap.MethodGET:
		log.Debug.Printf("%v GET /characteristics", request.RemoteAddr)
		request.ParseForm()
		res, err = handler.controller.HandleGetCharacteristics(request.Form)
	case hap.MethodPUT:
		log.Debug.Printf("%v PUT /characteristics", request.RemoteAddr)
		session := handler.context.GetSessionForRequest(request)
		conn := session.Connection()
		err = handler.controller.HandleUpdateCharacteristics(request.Body, conn)
	default:
		log.Debug.Println("Cannot handle HTTP method", request.Method)
	}
	handler.mutex.Unlock()

	if err != nil {
		log.Info.Panic(err)
		response.WriteHeader(http.StatusInternalServerError)
	} else {
		if res != nil {
			response.Header().Set("Content-Type", hap.HTTPContentTypeHAPJson)
			wr := hap.NewChunkedWriter(response, 2048)
			b, _ := ioutil.ReadAll(res)
			wr.Write(b)
		} else {
			response.WriteHeader(http.StatusNoContent)
		}
	}
}
示例#2
0
func (handler *Accessories) ServeHTTP(response http.ResponseWriter, request *http.Request) {
	log.Debug.Printf("%v GET /accessories", request.RemoteAddr)
	response.Header().Set("Content-Type", hap.HTTPContentTypeHAPJson)

	handler.mutex.Lock()
	res, err := handler.controller.HandleGetAccessories(request.Body)
	handler.mutex.Unlock()

	if err != nil {
		log.Info.Panic(err)
		response.WriteHeader(http.StatusInternalServerError)
	} else {
		// Write the data in chunks of 2048 bytes
		// http.ResponseWriter should do this already, but crashes because of an unkown reason
		wr := hap.NewChunkedWriter(response, 2048)
		b, _ := ioutil.ReadAll(res)
		log.Debug.Println(string(b))
		_, err := wr.Write(b)
		if err != nil {
			log.Info.Panic(err)
		}
	}
}