OCEntityHandlerResult handle_message(things_resource_s *target_resource) { OCEntityHandlerResult eh_result = OC_EH_ERROR; if (NULL == target_resource) { THINGS_LOG_D(TAG, "Request Item is NULL."); return OC_EH_ERROR; } THINGS_LOG_D(TAG, "Request Handle : %x, Resource Handle : %x", target_resource->request_handle, target_resource->resource_handle); if (target_resource->req_type == OC_REST_GET) { THINGS_LOG_V(TAG, "\t\tReq. : GET on %s", target_resource->uri); THINGS_LOG_V(TAG, "\t\tQuery: %s", target_resource->query); eh_result = process_get_request(target_resource); } else if (target_resource->req_type == OC_REST_POST) { THINGS_LOG_V(TAG, "\t\tReq. : POST on %s", target_resource->uri); THINGS_LOG_V(TAG, "\t\tQuery: %s", target_resource->query); eh_result = process_post_request(&target_resource); } else { THINGS_LOG_E(TAG, " Invalid Request Received : %d", target_resource->req_type); } THINGS_LOG_D(TAG, " @@@@@ target_resource ->size : %d", target_resource->size); if (is_can_not_response_case(target_resource, target_resource->req_type, eh_result) == false) { if (eh_result != OC_EH_OK && eh_result != OC_EH_SLOW) { THINGS_LOG_E(TAG, "Handing Request Failed, Sending ERROR Response"); send_response(target_resource->request_handle, target_resource->resource_handle, eh_result, target_resource->uri, NULL); eh_result = OC_EH_OK; } else { OCRepPayload *rep_payload = target_resource->things_get_rep_payload(target_resource); eh_result = send_response(target_resource->request_handle, // reqInfo->reqHandle, target_resource->resource_handle, // reqInfo->resHandle, target_resource->error, target_resource->uri, rep_payload); OCPayloadDestroy((OCPayload *) rep_payload); rep_payload = NULL; } } // Need to design How to release memory allocated for the things_resource_s list. things_release_resource_inst(target_resource); return eh_result; }
int process_request(FILE *f, char *root) { //TODO: divide into subfunctions char buf[1024]; char *method; char *relative_path; char *protocol; if (!fgets(buf, sizeof(buf), f)) return -1; log_info("%s", buf); //strtok - tokenizer strtok(NULL, " ") - takes another token method = strtok(buf, " "); relative_path = strtok(NULL, " "); protocol = strtok(NULL, "\r"); if (!method || !relative_path || !protocol) return -1; //moved after checking the relative path char *path = calloc(strlen(root) + strlen(relative_path) + 1, sizeof(char)); memmove(path, root, strlen(root)); path[strlen(root) + 1] = '\0'; strcat(path, relative_path); fseek(f, 0, SEEK_CUR); //change stream direction if (strcasecmp(method, "GET") == 0) { process_get_request(f, path, relative_path); } else { send_response(f, 501, "Not supported", NULL, "Method is not supported"); } free(path); return 0; }
/** * MHD_AccessHandlerCallback function that manages all connections requests * @param cls is the server_struct_t * server_struct main server * structure. * @todo . free some memory where needed * . manage errors codes */ static int ahc(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) { server_struct_t *ahc_server_struct = (server_struct_t *) cls; int success = MHD_NO; if (g_strcmp0(method, "GET") == 0) { /* We have a GET method that needs to be processed */ success = process_get_request(ahc_server_struct, connection, url, con_cls); } else if (g_strcmp0(method, "POST") == 0) { /* We have a POST method that needs to be processed */ success = process_post_request(ahc_server_struct, connection, url, con_cls, upload_data, upload_data_size); } else { /* not a GET nor a POST -> we do not know what to do ! */ success = MHD_NO; } return success; }