-
Notifications
You must be signed in to change notification settings - Fork 0
PR: gh #66 : Changes for GET Request support in ut-control #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
ae7c188
f62d964
6bd937b
a846b89
d710c52
5fedbd3
0db710e
6688fa3
1f31e44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -288,26 +288,246 @@ static int callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void | |
| return 0; | ||
| } | ||
| #else | ||
|
|
||
| static char* create_response(ut_cp_instance_internal_t *pInternal, const char* key, const char* type) | ||
| { | ||
| ut_kvp_instance_t *pkvpInstance = NULL; | ||
| ut_kvp_status_t status; | ||
| char result_kvp[UT_KVP_MAX_ELEMENT_SIZE] = {0xff}; | ||
| char* response = NULL; | ||
|
|
||
| for (uint32_t i = 0; i < pInternal->callback_entry_index; i++) | ||
| { | ||
| CallbackEntry_t entry = pInternal->callbackEntryList[i]; | ||
| void *userData = malloc(strlen(entry.userData) + 1); // +1 for null terminator | ||
| if (userData == NULL) | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("Memory allocation failed\n"); | ||
| return NULL; // Handle memory allocation failure | ||
| } | ||
| memcpy(userData, entry.userData, strlen(entry.userData) + 1); | ||
|
|
||
| pkvpInstance = ut_kvp_createInstance(); | ||
|
|
||
| /* Note: userData data will be freed by the destoryInstance() function */ | ||
| status = ut_kvp_openMemory(pkvpInstance, userData, strlen(entry.userData)); | ||
| if (status != UT_KVP_STATUS_SUCCESS) | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("ut_kvp_open() - Read Failure\n"); | ||
| ut_kvp_destroyInstance(pkvpInstance); | ||
| return NULL; | ||
| } | ||
| if (UT_KVP_STATUS_SUCCESS == ut_kvp_getStringField(pkvpInstance, key, result_kvp, UT_KVP_MAX_ELEMENT_SIZE)) | ||
| { | ||
| response = ut_kvp_getDataOfType(pkvpInstance, type); | ||
| } | ||
| ut_kvp_destroyInstance(pkvpInstance); | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| // Helper function to send error response | ||
| static int send_error_response(struct lws *wsi, int status, const char *content_type, const char *body) | ||
| { | ||
| unsigned char buffer[LWS_PRE + 1024]; | ||
| unsigned char *p = buffer + LWS_PRE, *end = buffer + sizeof(buffer); | ||
|
|
||
| // Add HTTP headers | ||
| if (lws_add_http_common_headers(wsi, status, content_type, strlen(body), &p, end) < 0) | ||
| return -1; | ||
|
|
||
| // Finalize headers | ||
| if (lws_finalize_http_header(wsi, &p, end) < 0) | ||
| return -1; | ||
|
|
||
| // Write headers | ||
| if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0) | ||
| return -1; | ||
|
|
||
| // Write body | ||
| if (lws_write(wsi, (unsigned char *)body, strlen(body), LWS_WRITE_HTTP_FINAL) < 0) | ||
| return -1; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) | ||
| { | ||
| cp_message_t msg; | ||
| ut_cp_instance_internal_t *pInternal = (ut_cp_instance_internal_t* )lws_context_user(lws_get_context(wsi)); | ||
| ut_cp_instance_internal_t *pInternal = (ut_cp_instance_internal_t *)lws_context_user(lws_get_context(wsi)); | ||
| struct per_session_data__http *perSessionData = (struct per_session_data__http *)user; | ||
|
|
||
| switch (reason) | ||
| { | ||
| case LWS_CALLBACK_HTTP: { | ||
|
|
||
| case LWS_CALLBACK_HTTP: | ||
| { | ||
| UT_CONTROL_PLANE_DEBUG("LWS_CALLBACK_HTTP\n"); | ||
| char *requested_uri = (char *)in; | ||
|
|
||
| char *requested_uri = (char *)in; // Use the 'in' parameter to get the URI | ||
| char query_string[256] = {0}; // Buffer for the query string | ||
| char accept_header[256] = {0}; // Buffer for the Accept header | ||
| char *response = NULL; | ||
| char *key = NULL; | ||
| unsigned char buffer[LWS_PRE + 1024]; // Allocate buffer for headers and body | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is very large for a local stack variable, on 8k stacks or 4k stacks you may have an issue. You should really allocate blocks bigger than 256 bytes as a general rule. and free it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should support large responses, see -> https://github.com/rdkcentral/rdk-halif-aidl/blob/main/panel/current/hfp-panel.yaml |
||
| unsigned char *p = buffer + LWS_PRE; // Pointer to start of usable buffer space | ||
| unsigned char *end = buffer + sizeof(buffer); // Pointer to end of buffer | ||
|
|
||
|
|
||
| UT_CONTROL_PLANE_DEBUG("Requested URI: %s\n", requested_uri); | ||
|
|
||
| // Handle GET request for /api/getKVP | ||
| if (strcmp(requested_uri, "/api/getKVP") == 0) | ||
kanjoe24 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // Extract the query string (if any) | ||
| if (lws_hdr_copy(wsi, query_string, sizeof(query_string), WSI_TOKEN_HTTP_URI_ARGS) > 0) | ||
| { | ||
| UT_CONTROL_PLANE_DEBUG("Query String: %s\n", query_string); | ||
|
|
||
| // Directly look for the "key=" prefix in the query string | ||
| char *key_param = strstr(query_string, "key="); | ||
| if (key_param != NULL) | ||
| { | ||
| key = key_param + 4; // Extract the value part (skip "key=") | ||
| UT_CONTROL_PLANE_DEBUG("Parsed Query Parameter: key=%s\n", key); | ||
| } | ||
| else | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("Query parameter 'key' not found\n"); | ||
| key = NULL; // Ensure key is NULL if not found | ||
| } | ||
| } | ||
| else | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("Failed to extract query string\n"); | ||
| key = NULL; // Ensure key is NULL if query string is absent | ||
| } | ||
|
|
||
| // Extract the Accept header | ||
| if (lws_hdr_copy(wsi, accept_header, sizeof(accept_header), WSI_TOKEN_HTTP_ACCEPT) > 0) | ||
| { | ||
| UT_CONTROL_PLANE_DEBUG("Accept Header: %s\n", accept_header); | ||
| } | ||
| else | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("Missing Accept header\n"); | ||
| const char *error_response = "{\"error\": \"Missing Accept header\"}"; | ||
| if (send_error_response(wsi, HTTP_STATUS_BAD_REQUEST, "application/json", error_response) < 0) | ||
| return -1; | ||
| return -1; | ||
| } | ||
|
|
||
| // Check for valid key parameter | ||
| if (key) | ||
| { | ||
| if (strncmp(accept_header, "application/json", 16) == 0) | ||
| { | ||
| response = create_response(pInternal, key, "json"); | ||
|
|
||
| if (response == NULL) | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("Failed to create response\n"); | ||
| const char *error_response = "{\"error\": \"Internal Server Error\"}"; | ||
| if (send_error_response(wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, "application/json", error_response) < 0) | ||
| return -1; | ||
| return -1; | ||
| } | ||
|
|
||
| // Add HTTP headers | ||
| if (lws_add_http_common_headers(wsi, HTTP_STATUS_OK, "application/json", strlen(response), &p, end) < 0) | ||
| { | ||
| return -1; // Failed to add headers | ||
| } | ||
|
|
||
| // Finalize headers | ||
| if (lws_finalize_http_header(wsi, &p, end) < 0) | ||
| { | ||
| return -1; // Failed to finalize headers | ||
| } | ||
|
|
||
| // Write headers | ||
| if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0) | ||
| { | ||
| return -1; // Failed to write headers | ||
| } | ||
|
|
||
| // Write body | ||
| if (lws_write(wsi, (unsigned char *)response, strlen(response), LWS_WRITE_HTTP_FINAL) < 0) | ||
| { | ||
| return -1; // Failed to write body | ||
| } | ||
|
|
||
| free(response); | ||
|
|
||
| return 1; // HTTP request handled | ||
| } | ||
| else if (strncmp(accept_header, "application/x-yaml", 18) == 0) | ||
| { | ||
| response = create_response(pInternal, key, "yaml"); | ||
|
|
||
| if (response == NULL) | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("Failed to create response\n"); | ||
| const char *error_response = "error: Internal Server Error\n"; | ||
| if (send_error_response(wsi, HTTP_STATUS_INTERNAL_SERVER_ERROR, "application/x-yaml", error_response) < 0) | ||
| return -1; | ||
| return -1; | ||
| } | ||
|
|
||
| // Add HTTP headers | ||
| if (lws_add_http_common_headers(wsi, HTTP_STATUS_OK, "application/json", strlen(response), &p, end) < 0) | ||
| { | ||
| return -1; // Failed to add headers | ||
| } | ||
|
|
||
| // Finalize headers | ||
| if (lws_finalize_http_header(wsi, &p, end) < 0) | ||
| { | ||
| return -1; // Failed to finalize headers | ||
| } | ||
|
|
||
| // Write headers | ||
| if (lws_write(wsi, buffer + LWS_PRE, p - (buffer + LWS_PRE), LWS_WRITE_HTTP_HEADERS) < 0) | ||
| { | ||
| return -1; // Failed to write headers | ||
| } | ||
|
|
||
| // Write body | ||
| if (lws_write(wsi, (unsigned char *)response, strlen(response), LWS_WRITE_HTTP_FINAL) < 0) | ||
| { | ||
| return -1; // Failed to write body | ||
| } | ||
|
|
||
| free(response); | ||
|
|
||
| return 1; // HTTP request handled | ||
| } | ||
| else | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("Invalid key value or unsupported Accept header\n"); | ||
| const char *error_response = "{\"error\": \"Unsupported Accept header or invalid type\"}"; | ||
| if (send_error_response(wsi, HTTP_STATUS_BAD_REQUEST, "application/json", error_response) < 0) | ||
| return -1; | ||
| return -1; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| UT_CONTROL_PLANE_ERROR("Missing or invalid key parameter\n"); | ||
| lws_return_http_status(wsi, HTTP_STATUS_BAD_REQUEST, NULL); | ||
| return -1; | ||
| } | ||
| } | ||
| // Handle POST request for /api/postKVP | ||
| if (strcmp(requested_uri, "/api/postKVP") == 0) | ||
kanjoe24 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| lws_callback_on_writable(wsi); | ||
| return 0; | ||
| return 0; // Let the body handling process continue | ||
| } | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| case LWS_CALLBACK_HTTP_BODY: | ||
| { | ||
| UT_CONTROL_PLANE_DEBUG("LWS_CALLBACK_HTTP\n"); | ||
|
|
@@ -322,6 +542,7 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void | |
| else | ||
| { | ||
| // POST data too large | ||
| UT_CONTROL_PLANE_ERROR("POST data too large\n"); | ||
| return -1; | ||
| } | ||
| } | ||
|
|
@@ -351,12 +572,13 @@ static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void | |
| // lws_write(wsi, (unsigned char *)response, strlen(response), LWS_WRITE_HTTP); | ||
| return 1; // HTTP request handled | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
| return 0; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.