From d000d17feac54caa934885840f1d60709751569a Mon Sep 17 00:00:00 2001 From: Tibo-lg Date: Tue, 22 Apr 2014 14:10:55 +0200 Subject: [PATCH] Quick fix. --- OperationHandler/homeport/src/homeport.c | 43 ++++++++++++------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/OperationHandler/homeport/src/homeport.c b/OperationHandler/homeport/src/homeport.c index acfe4528..06753cb6 100644 --- a/OperationHandler/homeport/src/homeport.c +++ b/OperationHandler/homeport/src/homeport.c @@ -48,7 +48,7 @@ char *timestamp ( void ); char* stateToXml(char *state); char* xmlToState(char *xml); char* stateToJson(char *state); -char* jsonToState(char *xml); +const char* jsonToState(char *xml); static void sig_cb ( struct ev_loop *loop, struct ev_signal *w, int revents ); char* generateUri( Device *device, Service *service ); @@ -261,31 +261,32 @@ homePortSendState(Service *service, void *req_in, const char *val, size_t len) struct lm *headers; // Call callback and send response - buffer = malloc((len+1) * sizeof(char)); if (len) { + buffer = malloc((len+1) * sizeof(char)); + strncpy(buffer, val, len); buffer[len] = '\0'; /*TODO Check header for XML or jSON*/ char *accept = lm_find( headersIn, "Accept" ); - if( strcmp(accept, "application/json") == 0 ) - { - state = stateToJson(buffer); - headers = lm_create(); - lm_insert(headers, "Content-Type", "application/json"); - lr_sendf(req, WS_HTTP_200, headers, state); - } - else - { - state = stateToXml(buffer); - headers = lm_create(); - lm_insert(headers, "Content-Type", "application/xml"); - lr_sendf(req, WS_HTTP_200, headers, state); - } + if( strcmp(accept, "application/json") == 0 ) + { + state = stateToJson(buffer); + headers = lm_create(); + lm_insert(headers, "Content-Type", "application/json"); + lr_sendf(req, WS_HTTP_200, headers, state); + } + else + { + state = stateToXml(buffer); + headers = lm_create(); + lm_insert(headers, "Content-Type", "application/xml"); + lr_sendf(req, WS_HTTP_200, headers, state); + } lm_destroy(headers); free(state); + free(buffer); } else { lr_sendf(req, WS_HTTP_500, NULL, "Internal Server Error"); } - free(buffer); } int @@ -334,7 +335,6 @@ homePortSetState(void *srv_data, void **req_data, *req_data = str; return 0; } - struct lm *headers; struct lm *headersIn = lr_request_get_headers(req); char *contentType = lm_find(headersIn, "Content-Type"); char *value; @@ -370,6 +370,7 @@ homePortSetState(void *srv_data, void **req_data, int buf_len = service->putFunction(service, buffer, MHD_MAX_BUFFER_SIZE, value); + if(freeValue) free(value); // Send response @@ -384,7 +385,7 @@ homePortSetState(void *srv_data, void **req_data, // send_event_of_value_change(service, buffer, IP); // Reply to request - const char *ret; + char *ret; if( strcmp( contentType, "application/xml" ) == 0 ) { ret = stateToXml(buffer); @@ -588,7 +589,7 @@ stateToJson(char *state) return NULL; } -char* +const char* jsonToState(char *json_value) { json_t *json = NULL; @@ -610,7 +611,7 @@ jsonToState(char *json_value) goto error; } - char *ret = json_string_value(value); + const char *ret = json_string_value(value); json_decref(json);