diff --git a/src/http_req.c b/src/http_req.c index 9573328b..b971e6a6 100644 --- a/src/http_req.c +++ b/src/http_req.c @@ -145,7 +145,7 @@ http_req_header_get_field(const http_req_header_t req_header, const char* const { return NULL; } - *p_len = (uint32_t)(ptrdiff_t)(p_end - p_val); + *p_len = (uint32_t)(p_end - p_val); } else { @@ -154,7 +154,7 @@ http_req_header_get_field(const http_req_header_t req_header, const char* const { return NULL; } - *p_len = (uint32_t)(ptrdiff_t)(p_end - p_val); + *p_len = (uint32_t)(p_end - p_val); } return p_val; diff --git a/src/http_server_accept_and_handle_conn.c b/src/http_server_accept_and_handle_conn.c index ae18f861..bd248533 100644 --- a/src/http_server_accept_and_handle_conn.c +++ b/src/http_server_accept_and_handle_conn.c @@ -52,7 +52,7 @@ get_http_body(const char* const p_msg, const uint32_t len, uint32_t* const p_bod p_body += strlen(g_newlines); if (NULL != p_body_len) { - *p_body_len = len - (uint32_t)(ptrdiff_t)(p_body - p_msg); + *p_body_len = len - (uint32_t)(p_body - p_msg); } return p_body; } @@ -217,7 +217,7 @@ http_server_netconn_write( offset += bytes_written; int32_t send_timeout = p_conn->send_timeout; bool flag_success = false; - while (!flag_success && (send_timeout > 0)) + while ((!flag_success) && (send_timeout > 0)) { const int32_t tmp_timeout = (send_timeout > 1000) ? 1000 : send_timeout; flag_success = http_server_sema_send_wait_timeout(tmp_timeout); @@ -476,6 +476,80 @@ http_server_write_content(struct netconn* const p_conn, http_server_resp_t* cons } } +static void +http_server_netconn_resp_content_with_len( + struct netconn* const p_conn, + const http_server_resp_t* const p_resp, + const http_header_extra_fields_t* const p_extra_header_fields, + const http_resp_code_e resp_code, + const char* const p_status_msg, + const bool use_extra_content_type_param, + const http_header_date_str_t* const p_date_str) +{ + if (!http_server_netconn_printf( + p_conn, + true, + "HTTP/1.0 %u %s\r\n" + "Server: Ruuvi Gateway\r\n" + "%s" + "Content-type: %s; charset=utf-8%s%s\r\n" + "Content-Length: %lu\r\n" + "%s" + "%s" + "%s" + "\r\n", + (printf_uint_t)resp_code, + p_status_msg, + p_date_str->buf, + http_get_content_type_str(p_resp->content_type), + use_extra_content_type_param ? "; " : "", + use_extra_content_type_param ? p_resp->p_content_type_param : "", + (printf_ulong_t)p_resp->content_len, + (NULL != p_extra_header_fields) ? p_extra_header_fields->buf : "", + http_get_content_encoding_str(p_resp), + http_get_cache_control_str(p_resp))) + { + LOG_ERR("%s failed", "http_server_netconn_printf"); + return; + } +} + +static void +http_server_netconn_resp_content_without_len( + struct netconn* const p_conn, + const http_server_resp_t* const p_resp, + const http_header_extra_fields_t* const p_extra_header_fields, + const http_resp_code_e resp_code, + const char* const p_status_msg, + const bool use_extra_content_type_param, + const http_header_date_str_t* const p_date_str) +{ + if (!http_server_netconn_printf( + p_conn, + true, + "HTTP/1.0 %u %s\r\n" + "Server: Ruuvi Gateway\r\n" + "%s" + "Content-type: %s; charset=utf-8%s%s\r\n" + "%s" + "%s" + "%s" + "\r\n", + (printf_uint_t)resp_code, + p_status_msg, + p_date_str->buf, + http_get_content_type_str(p_resp->content_type), + use_extra_content_type_param ? "; " : "", + use_extra_content_type_param ? p_resp->p_content_type_param : "", + (NULL != p_extra_header_fields) ? p_extra_header_fields->buf : "", + http_get_content_encoding_str(p_resp), + http_get_cache_control_str(p_resp))) + { + LOG_ERR("%s failed", "http_server_netconn_printf"); + return; + } +} + static void http_server_netconn_resp_with_content( struct netconn* const p_conn, @@ -501,59 +575,25 @@ http_server_netconn_resp_with_content( const http_header_date_str_t date_str = http_server_gen_header_date_str(true); if (SIZE_MAX != p_resp->content_len) { - if (!http_server_netconn_printf( - p_conn, - true, - "HTTP/1.0 %u %s\r\n" - "Server: Ruuvi Gateway\r\n" - "%s" - "Content-type: %s; charset=utf-8%s%s\r\n" - "Content-Length: %lu\r\n" - "%s" - "%s" - "%s" - "\r\n", - (printf_uint_t)resp_code, - p_status_msg, - date_str.buf, - http_get_content_type_str(p_resp->content_type), - use_extra_content_type_param ? "; " : "", - use_extra_content_type_param ? p_resp->p_content_type_param : "", - (printf_ulong_t)p_resp->content_len, - (NULL != p_extra_header_fields) ? p_extra_header_fields->buf : "", - http_get_content_encoding_str(p_resp), - http_get_cache_control_str(p_resp))) - { - LOG_ERR("%s failed", "http_server_netconn_printf"); - return; - } + http_server_netconn_resp_content_with_len( + p_conn, + p_resp, + p_extra_header_fields, + resp_code, + p_status_msg, + use_extra_content_type_param, + &date_str); } else { - if (!http_server_netconn_printf( - p_conn, - true, - "HTTP/1.0 %u %s\r\n" - "Server: Ruuvi Gateway\r\n" - "%s" - "Content-type: %s; charset=utf-8%s%s\r\n" - "%s" - "%s" - "%s" - "\r\n", - (printf_uint_t)resp_code, - p_status_msg, - date_str.buf, - http_get_content_type_str(p_resp->content_type), - use_extra_content_type_param ? "; " : "", - use_extra_content_type_param ? p_resp->p_content_type_param : "", - (NULL != p_extra_header_fields) ? p_extra_header_fields->buf : "", - http_get_content_encoding_str(p_resp), - http_get_cache_control_str(p_resp))) - { - LOG_ERR("%s failed", "http_server_netconn_printf"); - return; - } + http_server_netconn_resp_content_without_len( + p_conn, + p_resp, + p_extra_header_fields, + resp_code, + p_status_msg, + use_extra_content_type_param, + &date_str); } http_server_write_content(p_conn, p_resp); @@ -661,18 +701,28 @@ http_server_netconn_resp_302_auth_html( } static void -http_server_netconn_resp_400(struct netconn* const p_conn, http_server_resp_t* const p_resp) +http_server_netconn_resp_with_code( + struct netconn* const p_conn, + http_server_resp_t* const p_resp, + const http_resp_code_e resp_code, + const char* const p_status_msg) { if ((NULL == p_resp) || (0 == p_resp->content_len)) { - http_server_netconn_resp_without_content(p_conn, HTTP_RESP_CODE_400, "Bad Request"); + http_server_netconn_resp_without_content(p_conn, resp_code, p_status_msg); } else { - http_server_netconn_resp_with_content(p_conn, p_resp, NULL, HTTP_RESP_CODE_400, "Bad Request"); + http_server_netconn_resp_with_content(p_conn, p_resp, NULL, resp_code, p_status_msg); } } +static void +http_server_netconn_resp_400(struct netconn* const p_conn, http_server_resp_t* const p_resp) +{ + http_server_netconn_resp_with_code(p_conn, p_resp, HTTP_RESP_CODE_400, "Bad Request"); +} + static void http_server_netconn_resp_401( struct netconn* const p_conn, @@ -694,79 +744,37 @@ http_server_netconn_resp_403( static void http_server_netconn_resp_404(struct netconn* const p_conn, http_server_resp_t* const p_resp) { - if ((NULL == p_resp) || (0 == p_resp->content_len)) - { - http_server_netconn_resp_without_content(p_conn, HTTP_RESP_CODE_404, "Not Found"); - } - else - { - http_server_netconn_resp_with_content(p_conn, p_resp, NULL, HTTP_RESP_CODE_404, "Not Found"); - } + http_server_netconn_resp_with_code(p_conn, p_resp, HTTP_RESP_CODE_404, "Not Found"); } static void http_server_netconn_resp_429(struct netconn* const p_conn, http_server_resp_t* const p_resp) { - if ((NULL == p_resp) || (0 == p_resp->content_len)) - { - http_server_netconn_resp_without_content(p_conn, HTTP_RESP_CODE_429, "Too Many Requests"); - } - else - { - http_server_netconn_resp_with_content(p_conn, p_resp, NULL, HTTP_RESP_CODE_429, "Too Many Requests"); - } + http_server_netconn_resp_with_code(p_conn, p_resp, HTTP_RESP_CODE_429, "Too Many Requests"); } static void http_server_netconn_resp_500(struct netconn* const p_conn, http_server_resp_t* const p_resp) { - if ((NULL == p_resp) || (0 == p_resp->content_len)) - { - http_server_netconn_resp_without_content(p_conn, HTTP_RESP_CODE_500, "Internal Server Error"); - } - else - { - http_server_netconn_resp_with_content(p_conn, p_resp, NULL, HTTP_RESP_CODE_500, "Internal Server Error"); - } + http_server_netconn_resp_with_code(p_conn, p_resp, HTTP_RESP_CODE_500, "Internal Server Error"); } static void http_server_netconn_resp_502(struct netconn* const p_conn, http_server_resp_t* const p_resp) { - if ((NULL == p_resp) || (0 == p_resp->content_len)) - { - http_server_netconn_resp_without_content(p_conn, HTTP_RESP_CODE_502, "Bad Gateway"); - } - else - { - http_server_netconn_resp_with_content(p_conn, p_resp, NULL, HTTP_RESP_CODE_502, "Bad Gateway"); - } + http_server_netconn_resp_with_code(p_conn, p_resp, HTTP_RESP_CODE_502, "Bad Gateway"); } static void http_server_netconn_resp_503(struct netconn* const p_conn, http_server_resp_t* const p_resp) { - if ((NULL == p_resp) || (0 == p_resp->content_len)) - { - http_server_netconn_resp_without_content(p_conn, HTTP_RESP_CODE_503, "Service Unavailable"); - } - else - { - http_server_netconn_resp_with_content(p_conn, p_resp, NULL, HTTP_RESP_CODE_503, "Service Unavailable"); - } + http_server_netconn_resp_with_code(p_conn, p_resp, HTTP_RESP_CODE_503, "Service Unavailable"); } static void http_server_netconn_resp_504(struct netconn* const p_conn, http_server_resp_t* const p_resp) { - if ((NULL == p_resp) || (0 == p_resp->content_len)) - { - http_server_netconn_resp_without_content(p_conn, HTTP_RESP_CODE_504, "Gateway timeout"); - } - else - { - http_server_netconn_resp_with_content(p_conn, p_resp, NULL, HTTP_RESP_CODE_504, "Gateway timeout"); - } + http_server_netconn_resp_with_code(p_conn, p_resp, HTTP_RESP_CODE_504, "Gateway timeout"); } static void @@ -879,12 +887,14 @@ http_server_netconn_serve_handle_req( g_http_server_extra_header_fields.buf[0] = '\0'; - http_server_resp_t resp = http_server_handle_req( - &req_info, - p_remote_ip_str, - http_server_get_auth(), - &g_http_server_extra_header_fields, - flag_access_from_lan); + const http_server_handle_req_param_t param = { + .p_req_info = &req_info, + .p_remote_ip = p_remote_ip_str, + .p_auth_info = http_server_get_auth(), + .flag_access_from_lan = flag_access_from_lan, + }; + + http_server_resp_t resp = http_server_handle_req(¶m, &g_http_server_extra_header_fields); if ('\0' != g_http_server_extra_header_fields.buf[0]) { LOG_INFO("Extra HTTP-header resp: %s", g_http_server_extra_header_fields.buf); @@ -993,14 +1003,11 @@ http_server_accept_and_handle_conn(struct netconn* const p_conn) struct netconn* p_new_conn = NULL; os_mutex_t p_mutex = g_p_mutex_accept_conn; - if (NULL != p_mutex) + if ((NULL != p_mutex) && (!os_mutex_try_lock(p_mutex))) { - if (!os_mutex_try_lock(p_mutex)) - { - LOG_DBG("Can't lock mutex, sleep for %u ms", HTTP_SERVER_ACCEPT_DELAY_MS); - vTaskDelay(pdMS_TO_TICKS(HTTP_SERVER_ACCEPT_DELAY_MS)); - return; - } + LOG_DBG("Can't lock mutex, sleep for %u ms", HTTP_SERVER_ACCEPT_DELAY_MS); + vTaskDelay(pdMS_TO_TICKS(HTTP_SERVER_ACCEPT_DELAY_MS)); + return; } const err_t err = netconn_accept(p_conn, &p_new_conn); diff --git a/src/http_server_auth_digest.c b/src/http_server_auth_digest.c index 9699e857..fa45ba53 100644 --- a/src/http_server_auth_digest.c +++ b/src/http_server_auth_digest.c @@ -25,14 +25,14 @@ http_server_parse_token( return false; } p_idx1 += strlen(p_prefix); - const ptrdiff_t ptr_diff = (ptrdiff_t)(p_idx1 - p_str); + const ptrdiff_t ptr_diff = p_idx1 - p_str; const size_t len_diff = len - ptr_diff; const char* const p_idx2 = http_server_strnstr(p_idx1, p_suffix, len_diff); if (NULL == p_idx2) { return false; } - const size_t token_len = (size_t)(ptrdiff_t)(p_idx2 - p_idx1); + const size_t token_len = (size_t)(p_idx2 - p_idx1); if (token_len >= buf_size) { return false; diff --git a/src/http_server_auth_ruuvi.c b/src/http_server_auth_ruuvi.c index 7399fa06..96e356e0 100644 --- a/src/http_server_auth_ruuvi.c +++ b/src/http_server_auth_ruuvi.c @@ -26,20 +26,20 @@ http_server_auth_ruuvi_get_cookie( { return false; } - const size_t len_till_eol = len_cookies - (ptrdiff_t)(p_cookie_begin - p_cookies); + const size_t len_till_eol = len_cookies - (p_cookie_begin - p_cookies); const char* p_end_of_cookie = http_server_strnstr(p_cookie_begin, ";", len_till_eol); if (NULL == p_end_of_cookie) { p_end_of_cookie = p_cookie_begin + len_till_eol; } - const size_t len_of_cookie_pair = (size_t)(ptrdiff_t)(p_end_of_cookie - p_cookie_begin); + const size_t len_of_cookie_pair = (size_t)(p_end_of_cookie - p_cookie_begin); const char* const p_delimiter = http_server_strnstr(p_cookie_begin, "=", len_of_cookie_pair); if ((NULL == p_delimiter) || (p_delimiter > p_end_of_cookie)) { return false; } const char* const p_cookie_value = p_delimiter + 1; - const size_t cookie_len = (size_t)(ptrdiff_t)(p_end_of_cookie - p_cookie_value); + const size_t cookie_len = (size_t)(p_end_of_cookie - p_cookie_value); if (0 == cookie_len) { return false; @@ -131,11 +131,12 @@ http_server_auth_ruuvi_get_prev_url_from_cookies(const http_req_header_t http_he const char* const p_cookies = http_req_header_get_field(http_header, "Cookie:", &len_cookie); if (NULL == p_cookies) { + prev_url.buf[0] = '\0'; return prev_url; } if (!http_server_auth_ruuvi_get_ruuvi_prev_url_from_cookies(p_cookies, len_cookie, &prev_url)) { - return prev_url; + prev_url.buf[0] = '\0'; } return prev_url; } diff --git a/src/http_server_handle_req.c b/src/http_server_handle_req.c index 14468e0c..48246630 100644 --- a/src/http_server_handle_req.c +++ b/src/http_server_handle_req.c @@ -70,33 +70,31 @@ http_server_gen_resp_status_json(const json_network_info_t* const p_info, void* static http_server_resp_t http_server_handle_req_get( - const char* const p_file_name_unchecked, - const char* const p_uri_params, - const bool flag_access_from_lan, - const http_req_header_t http_header, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - http_header_extra_fields_t* const p_extra_header_fields) + const char* const p_file_name_unchecked, + const http_server_handle_req_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields) { + const char* const p_uri_params = p_param->p_req_info->http_uri_params.ptr; + const http_req_header_t http_header = p_param->p_req_info->http_header; + LOG_DBG("http_server_handle_req_get /%s", p_file_name_unchecked); const char* const p_file_name = (0 == strcmp(p_file_name_unchecked, "")) ? "index.html" : p_file_name_unchecked; - const wifiman_hostinfo_t hostinfo = wifiman_config_sta_get_hostinfo(); + const wifiman_hostinfo_t host_info = wifiman_config_sta_get_hostinfo(); if (0 == strcmp(p_file_name, "auth")) { - const bool flag_check_rw_access_with_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = p_param->flag_access_from_lan, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = p_param->p_remote_ip, + .p_auth_info = p_param->p_auth_info, + .p_hostinfo = &host_info, + }; - const http_server_resp_t resp_auth = http_server_handle_req_get_auth( - flag_access_from_lan, - flag_check_rw_access_with_bearer_token, - http_header, - p_remote_ip, - p_auth_info, - &hostinfo, - p_extra_header_fields); - return resp_auth; + return http_server_handle_req_get_auth(¶m, p_extra_header_fields); } bool flag_check_rw_access_with_bearer_token = false; @@ -110,20 +108,24 @@ http_server_handle_req_get( { bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = p_param->flag_access_from_lan, + .flag_check_rw_access_with_bearer_token = flag_check_rw_access_with_bearer_token, + .http_header = http_header, + .p_remote_ip = p_param->p_remote_ip, + .p_auth_info = p_param->p_auth_info, + .p_hostinfo = &host_info, + }; + const http_server_resp_t resp_auth_check = http_server_handle_req_check_auth( - flag_access_from_lan, - flag_check_rw_access_with_bearer_token, - http_header, - p_remote_ip, - p_auth_info, - &hostinfo, + ¶m, p_extra_header_fields, &flag_access_by_bearer_token); if ((!flag_access_by_bearer_token) && (HTTP_RESP_CODE_401 == resp_auth_check.http_resp_code) - && ((HTTP_SERVER_AUTH_TYPE_RUUVI == p_auth_info->auth_type) - || (HTTP_SERVER_AUTH_TYPE_DEFAULT == p_auth_info->auth_type))) + && ((HTTP_SERVER_AUTH_TYPE_RUUVI == p_param->p_auth_info->auth_type) + || (HTTP_SERVER_AUTH_TYPE_DEFAULT == p_param->p_auth_info->auth_type))) { - if ((0 != strcmp(p_file_name, "ap.json") && (0 != strcmp(p_file_name, "status.json")))) + if ((0 != strcmp(p_file_name, "ap.json")) && (0 != strcmp(p_file_name, "status.json"))) { (void)snprintf( p_extra_header_fields->buf, @@ -157,7 +159,7 @@ http_server_handle_req_get( http_server_resp_t http_resp = { 0 }; http_server_gen_resp_status_json_param_t params = { .p_http_resp = &http_resp, - .flag_access_from_lan = flag_access_from_lan, + .flag_access_from_lan = p_param->flag_access_from_lan, }; const os_delta_ticks_t ticks_to_wait = 10U; json_network_info_do_const_action_with_timeout(&http_server_gen_resp_status_json, ¶ms, ticks_to_wait); @@ -165,31 +167,34 @@ http_server_handle_req_get( return http_resp; } - return wifi_manager_cb_on_http_get(p_file_name, p_uri_params, flag_access_from_lan, NULL); + return wifi_manager_cb_on_http_get(p_file_name, p_uri_params, p_param->flag_access_from_lan, NULL); } static http_server_resp_t http_server_handle_req_delete( - const char* const p_file_name, - const char* const p_uri_params, - const bool flag_access_from_lan, - const http_req_header_t http_header, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - http_header_extra_fields_t* const p_extra_header_fields) + const char* const p_file_name, + const http_server_handle_req_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields) { + const char* const p_uri_params = p_param->p_req_info->http_uri_params.ptr; + const http_req_header_t http_header = p_param->p_req_info->http_header; + LOG_INFO("DELETE /%s, params=%s", p_file_name, (NULL != p_uri_params) ? p_uri_params : ""); - const wifiman_hostinfo_t hostinfo = wifiman_config_sta_get_hostinfo(); + const wifiman_hostinfo_t host_info = wifiman_config_sta_get_hostinfo(); - const bool flag_check_rw_access_with_bearer_token = true; - bool flag_access_by_bearer_token = false; - const http_server_resp_t resp_auth_check = http_server_handle_req_check_auth( - flag_access_from_lan, - flag_check_rw_access_with_bearer_token, - http_header, - p_remote_ip, - p_auth_info, - &hostinfo, + bool flag_access_by_bearer_token = false; + + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = p_param->flag_access_from_lan, + .flag_check_rw_access_with_bearer_token = true, + .http_header = http_header, + .p_remote_ip = p_param->p_remote_ip, + .p_auth_info = p_param->p_auth_info, + .p_hostinfo = &host_info, + }; + + const http_server_resp_t resp_auth_check = http_server_handle_req_check_auth( + ¶m, p_extra_header_fields, &flag_access_by_bearer_token); @@ -200,7 +205,7 @@ http_server_handle_req_delete( if (0 == strcmp(p_file_name, "auth")) { - return http_server_handle_req_delete_auth(http_header, p_remote_ip, p_auth_info, &hostinfo); + return http_server_handle_req_delete_auth(http_header, p_param->p_remote_ip, p_param->p_auth_info, &host_info); } if (0 == strcmp(p_file_name, "connect.json")) { @@ -217,7 +222,7 @@ http_server_handle_req_delete( } return http_server_resp_200_json("{}"); } - return wifi_manager_cb_on_http_delete(p_file_name, p_uri_params, flag_access_from_lan, NULL); + return wifi_manager_cb_on_http_delete(p_file_name, p_uri_params, p_param->flag_access_from_lan, NULL); } static const char* @@ -451,15 +456,14 @@ http_server_handle_req_post_connect_json(const http_req_body_t http_body) static http_server_resp_t http_server_handle_req_post( - const char* p_file_name, - const char* p_uri_params, - const bool flag_access_from_lan, - const http_req_header_t http_header, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - const http_req_body_t http_body, - http_header_extra_fields_t* const p_extra_header_fields) + const char* p_file_name, + const http_server_handle_req_param_t* const p_param, + const http_req_body_t http_body, + http_header_extra_fields_t* const p_extra_header_fields) { + const char* p_uri_params = p_param->p_req_info->http_uri_params.ptr; + const http_req_header_t http_header = p_param->p_req_info->http_header; + LOG_INFO("POST /%s, params=%s", p_file_name, (NULL != p_uri_params) ? p_uri_params : ""); const wifiman_hostinfo_t hostinfo = wifiman_config_sta_get_hostinfo(); @@ -467,24 +471,28 @@ http_server_handle_req_post( if (0 == strcmp(p_file_name, "auth")) { return http_server_handle_req_post_auth( - flag_access_from_lan, + p_param->flag_access_from_lan, http_header, - p_remote_ip, + p_param->p_remote_ip, http_body, - p_auth_info, + p_param->p_auth_info, &hostinfo, p_extra_header_fields); } - const bool flag_check_rw_access_with_bearer_token = true; - bool flag_access_by_bearer_token = false; - const http_server_resp_t resp_auth_check = http_server_handle_req_check_auth( - flag_access_from_lan, - flag_check_rw_access_with_bearer_token, - http_header, - p_remote_ip, - p_auth_info, - &hostinfo, + bool flag_access_by_bearer_token = false; + + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = p_param->flag_access_from_lan, + .flag_check_rw_access_with_bearer_token = true, + .http_header = http_header, + .p_remote_ip = p_param->p_remote_ip, + .p_auth_info = p_param->p_auth_info, + .p_hostinfo = &hostinfo, + }; + + const http_server_resp_t resp_auth_check = http_server_handle_req_check_auth( + ¶m, p_extra_header_fields, &flag_access_by_bearer_token); @@ -497,117 +505,112 @@ http_server_handle_req_post( { return http_server_handle_req_post_connect_json(http_body); } - return wifi_manager_cb_on_http_post(p_file_name, p_uri_params, http_body, flag_access_from_lan); + return wifi_manager_cb_on_http_post(p_file_name, p_uri_params, http_body, p_param->flag_access_from_lan); } -http_server_resp_t -http_server_handle_req( - const http_req_info_t* const p_req_info, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - http_header_extra_fields_t* const p_extra_header_fields, - const bool flag_access_from_lan) +static http_server_resp_t +http_server_handle_req_get_with_ecdh_key( + const char* const p_path, + const http_server_handle_req_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields) { - assert(NULL != p_extra_header_fields); - p_extra_header_fields->buf[0] = '\0'; + http_server_resp_t resp = http_server_handle_req_get(p_path, p_param, p_extra_header_fields); - const char* path = p_req_info->http_uri.ptr; - if ('/' == path[0]) - { - path += 1; - } + uint32_t len_ruuvi_ecdh_pub_key = 0; + const char* const p_ruuvi_ecdh_pub_key = http_req_header_get_field( + p_param->p_req_info->http_header, + "ruuvi_ecdh_pub_key:", + &len_ruuvi_ecdh_pub_key); - if (0 == strcmp("GET", p_req_info->http_cmd.ptr)) + if (NULL != p_ruuvi_ecdh_pub_key) { - http_server_resp_t resp = http_server_handle_req_get( - path, - p_req_info->http_uri_params.ptr, - flag_access_from_lan, - p_req_info->http_header, - p_remote_ip, - p_auth_info, - p_extra_header_fields); - - uint32_t len_ruuvi_ecdh_pub_key = 0; - const char* const p_ruuvi_ecdh_pub_key = http_req_header_get_field( - p_req_info->http_header, - "ruuvi_ecdh_pub_key:", - &len_ruuvi_ecdh_pub_key); - - if (NULL != p_ruuvi_ecdh_pub_key) + http_server_ecdh_pub_key_b64_t pub_key_b64_srv = { 0 }; + if (!http_server_handle_ruuvi_ecdh_pub_key(p_ruuvi_ecdh_pub_key, len_ruuvi_ecdh_pub_key, &pub_key_b64_srv)) { - http_server_ecdh_pub_key_b64_t pub_key_b64_srv = { 0 }; - if (!http_server_handle_ruuvi_ecdh_pub_key(p_ruuvi_ecdh_pub_key, len_ruuvi_ecdh_pub_key, &pub_key_b64_srv)) + LOG_ERR("http_server_handle_ruuvi_ecdh_pub_key failed"); + if ((HTTP_CONTENT_LOCATION_HEAP == resp.content_location) && (NULL != resp.select_location.memory.p_buf)) { - LOG_ERR("http_server_handle_ruuvi_ecdh_pub_key failed"); - if ((HTTP_CONTENT_LOCATION_HEAP == resp.content_location) - && (NULL != resp.select_location.memory.p_buf)) - { - os_free(resp.select_location.memory.p_buf); - } - return http_server_resp_500(); + os_free(resp.select_location.memory.p_buf); } - const size_t offset = strlen(p_extra_header_fields->buf); - (void)snprintf( - &p_extra_header_fields->buf[offset], - sizeof(p_extra_header_fields->buf) - offset, - "ruuvi_ecdh_pub_key: %s\r\n", - pub_key_b64_srv.buf); + return http_server_resp_500(); } - return resp; - } - if (0 == strcmp("DELETE", p_req_info->http_cmd.ptr)) + const size_t offset = strlen(p_extra_header_fields->buf); + (void)snprintf( + &p_extra_header_fields->buf[offset], + sizeof(p_extra_header_fields->buf) - offset, + "ruuvi_ecdh_pub_key: %s\r\n", + pub_key_b64_srv.buf); + } + return resp; +} + +static http_server_resp_t +http_server_handle_req_post_with_ecdh_key( + const char* const p_path, + const http_server_handle_req_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields) +{ + bool flag_encrypted = false; + uint32_t len_ruuvi_ecdh_encrypted = 0; + const char* const p_ruuvi_ecdh_encrypted = http_req_header_get_field( + p_param->p_req_info->http_header, + "ruuvi_ecdh_encrypted:", + &len_ruuvi_ecdh_encrypted); + if ((NULL != p_ruuvi_ecdh_encrypted) && (0 == strncmp(p_ruuvi_ecdh_encrypted, "true", len_ruuvi_ecdh_encrypted))) { - return http_server_handle_req_delete( - path, - p_req_info->http_uri_params.ptr, - flag_access_from_lan, - p_req_info->http_header, - p_remote_ip, - p_auth_info, - p_extra_header_fields); + flag_encrypted = true; } - if (0 == strcmp("POST", p_req_info->http_cmd.ptr)) + + str_buf_t decrypted_str_buf = STR_BUF_INIT_NULL(); + http_req_body_t http_body_decrypted = { + .ptr = NULL, + }; + if (flag_encrypted) { - bool flag_encrypted = false; - uint32_t len_ruuvi_ecdh_encrypted = 0; - const char* const p_ruuvi_ecdh_encrypted = http_req_header_get_field( - p_req_info->http_header, - "ruuvi_ecdh_encrypted:", - &len_ruuvi_ecdh_encrypted); - if ((NULL != p_ruuvi_ecdh_encrypted) - && (0 == strncmp(p_ruuvi_ecdh_encrypted, "true", len_ruuvi_ecdh_encrypted))) + if (!http_server_decrypt(p_param->p_req_info->http_body.ptr, &decrypted_str_buf)) { - flag_encrypted = true; + return http_server_resp_400(); } + http_body_decrypted.ptr = decrypted_str_buf.buf; + } - str_buf_t decrypted_str_buf = STR_BUF_INIT_NULL(); - http_req_body_t http_body_decrypted = { - .ptr = NULL, - }; - if (flag_encrypted) - { - if (!http_server_decrypt(p_req_info->http_body.ptr, &decrypted_str_buf)) - { - return http_server_resp_400(); - } - http_body_decrypted.ptr = decrypted_str_buf.buf; - } + const http_server_resp_t resp = http_server_handle_req_post( + p_path, + p_param, + flag_encrypted ? http_body_decrypted : p_param->p_req_info->http_body, + p_extra_header_fields); + if (flag_encrypted) + { + str_buf_free_buf(&decrypted_str_buf); + } + return resp; +} - const http_server_resp_t resp = http_server_handle_req_post( - path, - p_req_info->http_uri_params.ptr, - flag_access_from_lan, - p_req_info->http_header, - p_remote_ip, - p_auth_info, - flag_encrypted ? http_body_decrypted : p_req_info->http_body, - p_extra_header_fields); - if (flag_encrypted) - { - str_buf_free_buf(&decrypted_str_buf); - } - return resp; +http_server_resp_t +http_server_handle_req( + const http_server_handle_req_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields) +{ + assert(NULL != p_extra_header_fields); + p_extra_header_fields->buf[0] = '\0'; + + const char* p_path = p_param->p_req_info->http_uri.ptr; + if ('/' == p_path[0]) + { + p_path += 1; + } + + if (0 == strcmp("GET", p_param->p_req_info->http_cmd.ptr)) + { + return http_server_handle_req_get_with_ecdh_key(p_path, p_param, p_extra_header_fields); + } + if (0 == strcmp("DELETE", p_param->p_req_info->http_cmd.ptr)) + { + return http_server_handle_req_delete(p_path, p_param, p_extra_header_fields); + } + if (0 == strcmp("POST", p_param->p_req_info->http_cmd.ptr)) + { + return http_server_handle_req_post_with_ecdh_key(p_path, p_param, p_extra_header_fields); } return http_server_resp_400(); } diff --git a/src/http_server_handle_req.h b/src/http_server_handle_req.h index 8d872831..a8808a7d 100644 --- a/src/http_server_handle_req.h +++ b/src/http_server_handle_req.h @@ -16,13 +16,18 @@ extern "C" { #endif +typedef struct http_server_handle_req_param_t +{ + const http_req_info_t* const p_req_info; + const sta_ip_string_t* const p_remote_ip; + const http_server_auth_info_t* const p_auth_info; + const bool flag_access_from_lan; +} http_server_handle_req_param_t; + http_server_resp_t http_server_handle_req( - const http_req_info_t* const p_req_info, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - http_header_extra_fields_t* const p_extra_header_fields, - const bool flag_access_from_lan); + const http_server_handle_req_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields); #ifdef __cplusplus } diff --git a/src/http_server_handle_req_get_auth.c b/src/http_server_handle_req_get_auth.c index 1b3cf2c1..a7b5ca6b 100644 --- a/src/http_server_handle_req_get_auth.c +++ b/src/http_server_handle_req_get_auth.c @@ -81,66 +81,66 @@ http_server_handle_req_check_auth_bearer( static http_server_resp_t http_server_handle_req_get_auth_basic( - const bool flag_access_from_lan, - const http_req_header_t http_header, - const http_server_auth_info_t* const p_auth_info, - const wifiman_hostinfo_t* const p_hostinfo, - http_header_extra_fields_t* const p_extra_header_fields) + const http_server_handle_req_auth_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields) { uint32_t len_authorization = 0; - const char* const p_authorization = http_req_header_get_field(http_header, "Authorization:", &len_authorization); + const char* const p_authorization = http_req_header_get_field( + p_param->http_header, + "Authorization:", + &len_authorization); if (NULL == p_authorization) { - return http_server_resp_401_auth_basic(p_hostinfo, p_extra_header_fields); + return http_server_resp_401_auth_basic(p_param->p_hostinfo, p_extra_header_fields); } const char* const p_auth_prefix = "Basic "; const size_t auth_prefix_len = strlen(p_auth_prefix); if (0 != strncmp(p_authorization, p_auth_prefix, auth_prefix_len)) { - return http_server_resp_401_auth_basic(p_hostinfo, p_extra_header_fields); + return http_server_resp_401_auth_basic(p_param->p_hostinfo, p_extra_header_fields); } const char* const p_auth_token = &p_authorization[auth_prefix_len]; const size_t auth_token_len = len_authorization - auth_prefix_len; - if (auth_token_len != strlen(p_auth_info->auth_pass.buf)) + if (auth_token_len != strlen(p_param->p_auth_info->auth_pass.buf)) { - return http_server_resp_401_auth_basic(p_hostinfo, p_extra_header_fields); + return http_server_resp_401_auth_basic(p_param->p_hostinfo, p_extra_header_fields); } - if (0 != strncmp(p_auth_token, p_auth_info->auth_pass.buf, auth_token_len)) + if (0 != strncmp(p_auth_token, p_param->p_auth_info->auth_pass.buf, auth_token_len)) { - return http_server_resp_401_auth_basic(p_hostinfo, p_extra_header_fields); + return http_server_resp_401_auth_basic(p_param->p_hostinfo, p_extra_header_fields); } const http_server_resp_auth_json_t* p_auth_json = http_server_fill_auth_json( - p_hostinfo, + p_param->p_hostinfo, HTTP_SERVER_AUTH_TYPE_BASIC, - flag_access_from_lan, + p_param->flag_access_from_lan, NULL); return http_server_resp_200_json(p_auth_json->buf); } static http_server_resp_t http_server_handle_req_get_auth_digest( - const bool flag_access_from_lan, - const http_req_header_t http_header, - const http_server_auth_info_t* const p_auth_info, - const wifiman_hostinfo_t* const p_hostinfo, - http_header_extra_fields_t* const p_extra_header_fields) + const http_server_handle_req_auth_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields) { uint32_t len_authorization = 0; - const char* const p_authorization = http_req_header_get_field(http_header, "Authorization:", &len_authorization); + const char* const p_authorization = http_req_header_get_field( + p_param->http_header, + "Authorization:", + &len_authorization); if (NULL == p_authorization) { - return http_server_resp_401_auth_digest(p_hostinfo, p_extra_header_fields); + return http_server_resp_401_auth_digest(p_param->p_hostinfo, p_extra_header_fields); } http_server_auth_digest_req_t* const p_auth_req = http_server_auth_digest_get_info(); if (!http_server_parse_digest_authorization_str(p_authorization, len_authorization, p_auth_req)) { - return http_server_resp_401_auth_digest(p_hostinfo, p_extra_header_fields); + return http_server_resp_401_auth_digest(p_param->p_hostinfo, p_extra_header_fields); } - if (0 != strcmp(p_auth_req->username, p_auth_info->auth_user.buf)) + if (0 != strcmp(p_auth_req->username, p_param->p_auth_info->auth_user.buf)) { - return http_server_resp_401_auth_digest(p_hostinfo, p_extra_header_fields); + return http_server_resp_401_auth_digest(p_param->p_hostinfo, p_extra_header_fields); } str_buf_t str_buf = str_buf_printf_with_alloc("GET:%s", p_auth_req->uri); @@ -153,7 +153,7 @@ http_server_handle_req_get_auth_digest( str_buf = str_buf_printf_with_alloc( "%s:%s:%s:%s:%s:%s", - p_auth_info->auth_pass.buf, + p_param->p_auth_info->auth_pass.buf, p_auth_req->nonce, p_auth_req->nc, p_auth_req->cnonce, @@ -168,62 +168,59 @@ http_server_handle_req_get_auth_digest( if (0 != strcmp(p_auth_req->response, response_md5.buf)) { - return http_server_resp_401_auth_digest(p_hostinfo, p_extra_header_fields); + return http_server_resp_401_auth_digest(p_param->p_hostinfo, p_extra_header_fields); } const http_server_resp_auth_json_t* p_auth_json = http_server_fill_auth_json( - p_hostinfo, + p_param->p_hostinfo, HTTP_SERVER_AUTH_TYPE_DIGEST, - flag_access_from_lan, + p_param->flag_access_from_lan, NULL); return http_server_resp_200_json(p_auth_json->buf); } static http_server_resp_t http_server_handle_req_get_auth_ruuvi( - const bool flag_access_from_lan, - const http_req_header_t http_header, - const sta_ip_string_t* const p_remote_ip, - const wifiman_hostinfo_t* const p_hostinfo, - const bool flag_check, - const bool flag_auth_default, - http_header_extra_fields_t* const p_extra_header_fields) + const http_server_handle_req_auth_param_t* const p_param, + const bool flag_check, + const bool flag_auth_default, + http_header_extra_fields_t* const p_extra_header_fields) { http_server_auth_ruuvi_session_id_t session_id = { 0 }; - if (!http_server_auth_ruuvi_get_session_id_from_cookies(http_header, &session_id)) + if (!http_server_auth_ruuvi_get_session_id_from_cookies(p_param->http_header, &session_id)) { if (flag_check) { - return http_server_resp_401_auth_ruuvi(p_hostinfo, flag_auth_default); + return http_server_resp_401_auth_ruuvi(p_param->p_hostinfo, flag_auth_default); } return http_server_resp_401_auth_ruuvi_with_new_session_id( - p_remote_ip, - p_hostinfo, + p_param->p_remote_ip, + p_param->p_hostinfo, p_extra_header_fields, flag_auth_default, NULL); } const http_server_auth_ruuvi_authorized_session_t* const p_authorized_session - = http_server_auth_ruuvi_find_authorized_session(&session_id, p_remote_ip); + = http_server_auth_ruuvi_find_authorized_session(&session_id, p_param->p_remote_ip); if (NULL == p_authorized_session) { if (flag_check) { - return http_server_resp_401_auth_ruuvi(p_hostinfo, flag_auth_default); + return http_server_resp_401_auth_ruuvi(p_param->p_hostinfo, flag_auth_default); } return http_server_resp_401_auth_ruuvi_with_new_session_id( - p_remote_ip, - p_hostinfo, + p_param->p_remote_ip, + p_param->p_hostinfo, p_extra_header_fields, flag_auth_default, NULL); } const http_server_resp_auth_json_t* p_auth_json = http_server_fill_auth_json( - p_hostinfo, + p_param->p_hostinfo, flag_auth_default ? HTTP_SERVER_AUTH_TYPE_DEFAULT : HTTP_SERVER_AUTH_TYPE_RUUVI, - flag_access_from_lan, + p_param->flag_access_from_lan, NULL); return http_server_resp_200_json(p_auth_json->buf); } @@ -236,22 +233,17 @@ http_server_handle_req_get_auth_deny(const wifiman_hostinfo_t* const p_hostinfo) static http_server_resp_t http_server_handle_req_get_or_check_auth( - const bool flag_access_from_lan, - const http_req_header_t http_header, - const bool flag_check_rw_access_with_bearer_token, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - const wifiman_hostinfo_t* const p_hostinfo, - const bool flag_check, - http_header_extra_fields_t* const p_extra_header_fields, - bool* const p_flag_access_by_bearer_token) + const http_server_handle_req_auth_param_t* const p_param, + const bool flag_check, + http_header_extra_fields_t* const p_extra_header_fields, + bool* const p_flag_access_by_bearer_token) { if (NULL != p_flag_access_by_bearer_token) { http_server_auth_api_key_e access_by_bearer_token = http_server_handle_req_check_auth_bearer( - http_header, - flag_check_rw_access_with_bearer_token, - p_auth_info); + p_param->http_header, + p_param->flag_check_rw_access_with_bearer_token, + p_param->p_auth_info); switch (access_by_bearer_token) { case HTTP_SERVER_AUTH_API_KEY_NOT_USED: @@ -259,53 +251,35 @@ http_server_handle_req_get_or_check_auth( break; case HTTP_SERVER_AUTH_API_KEY_ALLOWED: *p_flag_access_by_bearer_token = true; - return http_server_resp_200_json( - http_server_fill_auth_json(p_hostinfo, HTTP_SERVER_AUTH_TYPE_BEARER, flag_access_from_lan, NULL) - ->buf); + return http_server_resp_200_json(http_server_fill_auth_json( + p_param->p_hostinfo, + HTTP_SERVER_AUTH_TYPE_BEARER, + p_param->flag_access_from_lan, + NULL) + ->buf); case HTTP_SERVER_AUTH_API_KEY_PROHIBITED: *p_flag_access_by_bearer_token = true; - return http_server_resp_401_json( - http_server_fill_auth_json(p_hostinfo, HTTP_SERVER_AUTH_TYPE_BEARER, flag_access_from_lan, NULL)); + return http_server_resp_401_json(http_server_fill_auth_json( + p_param->p_hostinfo, + HTTP_SERVER_AUTH_TYPE_BEARER, + p_param->flag_access_from_lan, + NULL)); } } - switch (p_auth_info->auth_type) + switch (p_param->p_auth_info->auth_type) { case HTTP_SERVER_AUTH_TYPE_ALLOW: - return http_server_handle_req_get_auth_allow(p_hostinfo, flag_access_from_lan); + return http_server_handle_req_get_auth_allow(p_param->p_hostinfo, p_param->flag_access_from_lan); case HTTP_SERVER_AUTH_TYPE_BASIC: - return http_server_handle_req_get_auth_basic( - flag_access_from_lan, - http_header, - p_auth_info, - p_hostinfo, - p_extra_header_fields); + return http_server_handle_req_get_auth_basic(p_param, p_extra_header_fields); case HTTP_SERVER_AUTH_TYPE_DIGEST: - return http_server_handle_req_get_auth_digest( - flag_access_from_lan, - http_header, - p_auth_info, - p_hostinfo, - p_extra_header_fields); + return http_server_handle_req_get_auth_digest(p_param, p_extra_header_fields); case HTTP_SERVER_AUTH_TYPE_RUUVI: - return http_server_handle_req_get_auth_ruuvi( - flag_access_from_lan, - http_header, - p_remote_ip, - p_hostinfo, - flag_check, - false, - p_extra_header_fields); + return http_server_handle_req_get_auth_ruuvi(p_param, flag_check, false, p_extra_header_fields); case HTTP_SERVER_AUTH_TYPE_DENY: - return http_server_handle_req_get_auth_deny(p_hostinfo); + return http_server_handle_req_get_auth_deny(p_param->p_hostinfo); case HTTP_SERVER_AUTH_TYPE_DEFAULT: - return http_server_handle_req_get_auth_ruuvi( - flag_access_from_lan, - http_header, - p_remote_ip, - p_hostinfo, - flag_check, - true, - p_extra_header_fields); + return http_server_handle_req_get_auth_ruuvi(p_param, flag_check, true, p_extra_header_fields); case HTTP_SERVER_AUTH_TYPE_BEARER: return http_server_resp_500(); } @@ -314,26 +288,16 @@ http_server_handle_req_get_or_check_auth( http_server_resp_t http_server_handle_req_check_auth( - const bool flag_access_from_lan, - const bool flag_check_rw_access_with_bearer_token, - const http_req_header_t http_header, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - const wifiman_hostinfo_t* const p_hostinfo, - http_header_extra_fields_t* const p_extra_header_fields, - bool* const p_flag_access_by_bearer_token) + const http_server_handle_req_auth_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields, + bool* const p_flag_access_by_bearer_token) { - if (!flag_access_from_lan) + if (!p_param->flag_access_from_lan) { - return http_server_handle_req_get_auth_allow(p_hostinfo, flag_access_from_lan); + return http_server_handle_req_get_auth_allow(p_param->p_hostinfo, p_param->flag_access_from_lan); } return http_server_handle_req_get_or_check_auth( - flag_access_from_lan, - http_header, - flag_check_rw_access_with_bearer_token, - p_remote_ip, - p_auth_info, - p_hostinfo, + p_param, true, p_extra_header_fields, p_flag_access_by_bearer_token); @@ -341,26 +305,12 @@ http_server_handle_req_check_auth( http_server_resp_t http_server_handle_req_get_auth( - const bool flag_access_from_lan, - const bool flag_check_rw_access_with_bearer_token, - const http_req_header_t http_header, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - const wifiman_hostinfo_t* const p_hostinfo, - http_header_extra_fields_t* const p_extra_header_fields) + const http_server_handle_req_auth_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields) { - if (!flag_access_from_lan) + if (!p_param->flag_access_from_lan) { - return http_server_handle_req_get_auth_allow(p_hostinfo, flag_access_from_lan); + return http_server_handle_req_get_auth_allow(p_param->p_hostinfo, p_param->flag_access_from_lan); } - return http_server_handle_req_get_or_check_auth( - flag_access_from_lan, - http_header, - flag_check_rw_access_with_bearer_token, - p_remote_ip, - p_auth_info, - p_hostinfo, - false, - p_extra_header_fields, - NULL); + return http_server_handle_req_get_or_check_auth(p_param, false, p_extra_header_fields, NULL); } diff --git a/src/http_server_handle_req_get_auth.h b/src/http_server_handle_req_get_auth.h index 0089b2b3..660c6583 100644 --- a/src/http_server_handle_req_get_auth.h +++ b/src/http_server_handle_req_get_auth.h @@ -24,26 +24,26 @@ typedef enum http_server_auth_api_key_e HTTP_SERVER_AUTH_API_KEY_PROHIBITED, } http_server_auth_api_key_e; +typedef struct http_server_handle_req_auth_param_t +{ + const bool flag_access_from_lan; + const bool flag_check_rw_access_with_bearer_token; + const http_req_header_t http_header; + const sta_ip_string_t* const p_remote_ip; + const http_server_auth_info_t* const p_auth_info; + const wifiman_hostinfo_t* const p_hostinfo; +} http_server_handle_req_auth_param_t; + http_server_resp_t http_server_handle_req_check_auth( - const bool flag_access_from_lan, - const bool flag_check_rw_access_with_bearer_token, - const http_req_header_t http_header, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - const wifiman_hostinfo_t* const p_hostinfo, - http_header_extra_fields_t* const p_extra_header_fields, - bool* const p_flag_access_by_bearer_token); + const http_server_handle_req_auth_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields, + bool* const p_flag_access_by_bearer_token); http_server_resp_t http_server_handle_req_get_auth( - const bool flag_access_from_lan, - const bool flag_check_rw_access_with_bearer_token, - const http_req_header_t http_header, - const sta_ip_string_t* const p_remote_ip, - const http_server_auth_info_t* const p_auth_info, - const wifiman_hostinfo_t* const p_hostinfo, - http_header_extra_fields_t* const p_extra_header_fields); + const http_server_handle_req_auth_param_t* const p_param, + http_header_extra_fields_t* const p_extra_header_fields); #ifdef __cplusplus } diff --git a/src/http_server_handle_req_post_auth.c b/src/http_server_handle_req_post_auth.c index f147b0f3..dd9c9461 100644 --- a/src/http_server_handle_req_post_auth.c +++ b/src/http_server_handle_req_post_auth.c @@ -181,73 +181,18 @@ http_server_handle_req_post_auth_check_login_session( } http_server_resp_t -http_server_handle_req_post_auth( - const bool flag_access_from_lan, - const http_req_header_t http_header, +http_server_handle_req_post_auth_check_auth( + http_server_auth_ruuvi_req_t* const p_auth_req, + const bool flag_auth_default, const sta_ip_string_t* const p_remote_ip, const http_req_body_t http_body, const http_server_auth_info_t* const p_auth_info, const wifiman_hostinfo_t* const p_hostinfo, http_header_extra_fields_t* const p_extra_header_fields) { - if (!flag_access_from_lan) - { - const http_server_resp_auth_json_t* p_auth_json = http_server_fill_auth_json( - p_hostinfo, - p_auth_info->auth_type, - flag_access_from_lan, - NULL); - return http_server_resp_200_json(p_auth_json->buf); - } - if ((HTTP_SERVER_AUTH_TYPE_RUUVI != p_auth_info->auth_type) - && (HTTP_SERVER_AUTH_TYPE_DEFAULT != p_auth_info->auth_type)) - { - LOG_ERR("Auth type is not RUUVI, auth_type=%d", (printf_int_t)p_auth_info->auth_type); - return http_server_resp_503(); - } - - const bool flag_auth_default = (HTTP_SERVER_AUTH_TYPE_DEFAULT == p_auth_info->auth_type) ? true : false; - - http_server_auth_ruuvi_session_id_t session_id = { 0 }; - if (!http_server_auth_ruuvi_get_session_id_from_cookies(http_header, &session_id)) - { - LOG_WARN("There is no session_id in cookies"); - return http_server_resp_401_auth_ruuvi_with_new_session_id( - p_remote_ip, - p_hostinfo, - p_extra_header_fields, - flag_auth_default, - NULL); - } - const http_server_auth_ruuvi_prev_url_t prev_url = http_server_auth_ruuvi_get_prev_url_from_cookies(http_header); - - http_server_auth_ruuvi_t* const p_auth_ruuvi = http_server_auth_ruuvi_get_info(); - - http_server_resp_t resp = { 0 }; - if (!http_server_handle_req_post_auth_check_login_session( - &p_auth_ruuvi->login_session, - &session_id, - p_remote_ip, - p_hostinfo, - flag_auth_default, - p_extra_header_fields, - &resp)) - { - return resp; - } - - http_server_auth_ruuvi_req_t* p_auth_req; - p_auth_req = os_calloc(1, sizeof(*p_auth_req)); - if (NULL == p_auth_req) - { - LOG_ERR("Can't allocate memory for auth_req"); - return http_server_resp_500(); - } - if (!json_ruuvi_auth_parse_http_body(http_body.ptr, p_auth_req)) { LOG_WARN("Failed to parse auth request body"); - os_free(p_auth_req); return http_server_resp_401_auth_ruuvi_with_new_session_id( p_remote_ip, p_hostinfo, @@ -258,7 +203,6 @@ http_server_handle_req_post_auth( if ('\0' == p_auth_req->username.buf[0]) { LOG_WARN("Username is empty"); - os_free(p_auth_req); return http_server_resp_401_auth_ruuvi_with_new_session_id( p_remote_ip, p_hostinfo, @@ -269,7 +213,6 @@ http_server_handle_req_post_auth( if (0 != strcmp(p_auth_info->auth_user.buf, p_auth_req->username.buf)) { LOG_WARN("Username in auth_info does not match the username from auth_req"); - os_free(p_auth_req); return http_server_resp_401_auth_ruuvi_with_new_session_id( p_remote_ip, p_hostinfo, @@ -281,16 +224,15 @@ http_server_handle_req_post_auth( if (NULL == p_password_hash) { LOG_ERR("Can't allocate memory"); - os_free(p_auth_req); return http_server_resp_500(); } + const http_server_auth_ruuvi_t* const p_auth_ruuvi = http_server_auth_ruuvi_get_info(); if (!http_server_auth_ruuvi_gen_hashed_password( p_auth_ruuvi->login_session.challenge.buf, p_auth_info->auth_pass.buf, p_password_hash)) { LOG_WARN("Failed to generate hashed password"); - os_free(p_auth_req); os_free(p_password_hash); return http_server_resp_401_auth_ruuvi_with_new_session_id( p_remote_ip, @@ -304,7 +246,6 @@ http_server_handle_req_post_auth( LOG_WARN("Password does not match"); LOG_DBG("Expected password: %s, actual password: %s", password_hash.buf, p_auth_req->password.buf); LOG_DBG("Challenge: %s, password: %s", p_auth_ruuvi->login_session.challenge.buf, p_auth_info->auth_pass.buf); - os_free(p_auth_req); os_free(p_password_hash); return http_server_resp_401_auth_ruuvi_with_new_session_id( @@ -314,8 +255,85 @@ http_server_handle_req_post_auth( flag_auth_default, "Incorrect password"); } - os_free(p_auth_req); os_free(p_password_hash); + return http_server_resp_200_json("{}"); +} + +http_server_resp_t +http_server_handle_req_post_auth( + const bool flag_access_from_lan, + const http_req_header_t http_header, + const sta_ip_string_t* const p_remote_ip, + const http_req_body_t http_body, + const http_server_auth_info_t* const p_auth_info, + const wifiman_hostinfo_t* const p_hostinfo, + http_header_extra_fields_t* const p_extra_header_fields) +{ + if (!flag_access_from_lan) + { + const http_server_resp_auth_json_t* p_auth_json = http_server_fill_auth_json( + p_hostinfo, + p_auth_info->auth_type, + flag_access_from_lan, + NULL); + return http_server_resp_200_json(p_auth_json->buf); + } + if ((HTTP_SERVER_AUTH_TYPE_RUUVI != p_auth_info->auth_type) + && (HTTP_SERVER_AUTH_TYPE_DEFAULT != p_auth_info->auth_type)) + { + LOG_ERR("Auth type is not RUUVI, auth_type=%d", (printf_int_t)p_auth_info->auth_type); + return http_server_resp_503(); + } + + const bool flag_auth_default = (HTTP_SERVER_AUTH_TYPE_DEFAULT == p_auth_info->auth_type) ? true : false; + + http_server_auth_ruuvi_session_id_t session_id = { 0 }; + if (!http_server_auth_ruuvi_get_session_id_from_cookies(http_header, &session_id)) + { + LOG_WARN("There is no session_id in cookies"); + return http_server_resp_401_auth_ruuvi_with_new_session_id( + p_remote_ip, + p_hostinfo, + p_extra_header_fields, + flag_auth_default, + NULL); + } + const http_server_auth_ruuvi_prev_url_t prev_url = http_server_auth_ruuvi_get_prev_url_from_cookies(http_header); + + http_server_auth_ruuvi_t* const p_auth_ruuvi = http_server_auth_ruuvi_get_info(); + + http_server_resp_t resp = { 0 }; + if (!http_server_handle_req_post_auth_check_login_session( + &p_auth_ruuvi->login_session, + &session_id, + p_remote_ip, + p_hostinfo, + flag_auth_default, + p_extra_header_fields, + &resp)) + { + return resp; + } + + http_server_auth_ruuvi_req_t* p_auth_req = os_calloc(1, sizeof(*p_auth_req)); + if (NULL == p_auth_req) + { + LOG_ERR("Can't allocate memory for auth_req"); + return http_server_resp_500(); + } + resp = http_server_handle_req_post_auth_check_auth( + p_auth_req, + flag_auth_default, + p_remote_ip, + http_body, + p_auth_info, + p_hostinfo, + p_extra_header_fields); + if (HTTP_RESP_CODE_200 != resp.http_resp_code) + { + return resp; + } + os_free(p_auth_req); http_server_auth_ruuvi_add_authorized_session(p_auth_ruuvi, &session_id, p_remote_ip); http_server_auth_ruuvi_login_session_clear(&p_auth_ruuvi->login_session); diff --git a/src/http_server_resp.c b/src/http_server_resp.c index 0715036f..00c0b5fa 100644 --- a/src/http_server_resp.c +++ b/src/http_server_resp.c @@ -33,15 +33,22 @@ http_server_resp_json_in_heap(const http_resp_code_e http_resp_code, const char* { const bool flag_no_cache = true; const bool flag_add_header_date = true; - return http_server_resp_data_in_heap( - http_resp_code, - HTTP_CONENT_TYPE_APPLICATION_JSON, - NULL, - strlen(p_json_content), - HTTP_CONENT_ENCODING_NONE, - (const uint8_t*)p_json_content, - flag_no_cache, - flag_add_header_date); + const http_server_resp_t resp = { + .http_resp_code = http_resp_code, + .content_location = HTTP_CONTENT_LOCATION_HEAP, + .flag_no_cache = flag_no_cache, + .flag_add_header_date = flag_add_header_date, + .content_type = HTTP_CONENT_TYPE_APPLICATION_JSON, + .p_content_type_param = NULL, + .content_len = strlen(p_json_content), + .content_encoding = HTTP_CONENT_ENCODING_NONE, + .select_location = { + .memory = { + .p_buf = (const uint8_t*)p_json_content, + }, + }, + }; + return resp; } http_server_resp_t @@ -267,8 +274,7 @@ http_server_resp_data_in_static_mem( } http_server_resp_t -http_server_resp_data_in_heap( - const http_resp_code_e resp_code, +http_server_resp_200_data_in_heap( const http_content_type_e content_type, const char* p_content_type_param, const size_t content_len, @@ -278,7 +284,7 @@ http_server_resp_data_in_heap( const bool flag_add_header_date) { const http_server_resp_t resp = { - .http_resp_code = resp_code, + .http_resp_code = HTTP_RESP_CODE_200, .content_location = HTTP_CONTENT_LOCATION_HEAP, .flag_no_cache = flag_no_cache, .flag_add_header_date = flag_add_header_date, @@ -295,27 +301,6 @@ http_server_resp_data_in_heap( return resp; } -http_server_resp_t -http_server_resp_200_data_in_heap( - const http_content_type_e content_type, - const char* p_content_type_param, - const size_t content_len, - const http_content_encoding_e content_encoding, - const uint8_t* p_buf, - const bool flag_no_cache, - const bool flag_add_header_date) -{ - return http_server_resp_data_in_heap( - HTTP_RESP_CODE_200, - content_type, - p_content_type_param, - content_len, - content_encoding, - p_buf, - flag_no_cache, - flag_add_header_date); -} - http_server_resp_t http_server_resp_data_from_file( http_resp_code_e http_resp_code, diff --git a/src/include/http_server_resp.h b/src/include/http_server_resp.h index d3f2d0ee..1a380b3a 100644 --- a/src/include/http_server_resp.h +++ b/src/include/http_server_resp.h @@ -94,17 +94,6 @@ http_server_resp_data_in_static_mem( const bool flag_no_cache, const bool flag_add_header_date); -http_server_resp_t -http_server_resp_data_in_heap( - const http_resp_code_e resp_code, - const http_content_type_e content_type, - const char* p_content_type_param, - const size_t content_len, - const http_content_encoding_e content_encoding, - const uint8_t* p_buf, - const bool flag_no_cache, - const bool flag_add_header_date); - http_server_resp_t http_server_resp_200_data_in_heap( const http_content_type_e content_type, diff --git a/src/json_access_points.c b/src/json_access_points.c index 5ca003bf..a6443642 100644 --- a/src/json_access_points.c +++ b/src/json_access_points.c @@ -36,16 +36,6 @@ Contains the freeRTOS task and all necessary support #include "wifi_manager_defs.h" #include "json.h" -void -json_access_points_init(void) -{ -} - -void -json_access_points_deinit(void) -{ -} - void json_access_points_generate_str_buf( str_buf_t* const p_str_buf, @@ -56,18 +46,18 @@ json_access_points_generate_str_buf( const uint32_t num_ap_checked = (num_access_points <= MAX_AP_NUM) ? num_access_points : MAX_AP_NUM; for (uint32_t i = 0; i < num_ap_checked; ++i) { - const wifi_ap_record_t ap = p_access_points[i]; + const wifi_ap_record_t* const p_ap = &p_access_points[i]; str_buf_printf(p_str_buf, "{\"ssid\":"); - json_print_escaped_string(p_str_buf, (const char*)ap.ssid); + json_print_escaped_string(p_str_buf, (const char*)p_ap->ssid); /* print the rest of the json for this access point: no more string to escape */ str_buf_printf( p_str_buf, ",\"chan\":%d,\"rssi\":%d,\"auth\":%d}%s\n", - ap.primary, - ap.rssi, - ap.authmode, + p_ap->primary, + p_ap->rssi, + p_ap->authmode, (i < (num_ap_checked - 1)) ? "," : ""); } str_buf_printf(p_str_buf, "]\n"); diff --git a/src/json_access_points.h b/src/json_access_points.h index c287c4b6..ba66862c 100644 --- a/src/json_access_points.h +++ b/src/json_access_points.h @@ -41,12 +41,6 @@ Contains the freeRTOS task and all necessary support extern "C" { #endif -void -json_access_points_init(void); - -void -json_access_points_deinit(void); - /** * @brief Generates the list of access points after a wifi scan. * @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful. diff --git a/src/wifi_manager.c b/src/wifi_manager.c index da38661a..da6ebabf 100644 --- a/src/wifi_manager.c +++ b/src/wifi_manager.c @@ -314,7 +314,6 @@ wifi_manager_task(void) esp_wifi_deinit(); /* heap buffers */ - json_access_points_deinit(); json_network_info_deinit(); sta_ip_safe_deinit(); diff --git a/src/wifi_manager_internal.c b/src/wifi_manager_internal.c index 0738d6ef..66a467d8 100644 --- a/src/wifi_manager_internal.c +++ b/src/wifi_manager_internal.c @@ -382,8 +382,6 @@ wifi_manager_init_start_wifi( return false; } - json_access_points_init(); - for (int32_t msg_code = WIFI_MAN_MSG_CODE_NONE; msg_code < MESSAGE_CODE_COUNT; ++msg_code) { wifi_manager_set_callback((message_code_e)msg_code, NULL); diff --git a/src/wifiman_config.c b/src/wifiman_config.c index fc200967..12f323b7 100644 --- a/src/wifiman_config.c +++ b/src/wifiman_config.c @@ -68,9 +68,8 @@ static const wifiman_config_t g_wifiman_config_default_const = { }, .sta = { .wifi_config_sta = { - .ssid = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, - .password = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, + .ssid = { 0 }, + .password = { 0 }, .scan_method = WIFI_FAST_SCAN, .bssid_set = false, .bssid = {0, 0, 0, 0, 0, 0}, @@ -90,11 +89,16 @@ static const wifiman_config_t g_wifiman_config_default_const = { .sta_power_save = WIFI_PS_NONE, .sta_static_ip = false, .sta_static_ip_config = { - .ip = { 0 }, - .netmask = { 0 }, - .gw = { 0 }, + .ip = { .addr = 0, }, + .netmask = { .addr = 0, }, + .gw = { .addr = 0, }, }, }, + .hostinfo = { + .hostname = { .buf = { 0 }, }, + .fw_ver = { .buf = { 0 }, }, + .nrf52_fw_ver = { .buf = { 0 }, }, + }, }, }; diff --git a/tests/ruuvi.json_stream_gen.c b/tests/ruuvi.json_stream_gen.c index 6aa3470c..8b43c7b7 160000 --- a/tests/ruuvi.json_stream_gen.c +++ b/tests/ruuvi.json_stream_gen.c @@ -1 +1 @@ -Subproject commit 6aa3470cc2754b449bb776fc72ea5aacec9baab8 +Subproject commit 8b43c7b7ddc2c6b12f95eb24c20aeda0c895ed4e diff --git a/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp b/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp index 102ac5b2..e7267836 100644 --- a/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp +++ b/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp @@ -107,15 +107,17 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_allow) // NOLINT const sta_ip_string_t remote_ip = { "192.168.1.10" }; http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -146,15 +148,17 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_allow_when_access_not_from_lan) const sta_ip_string_t remote_ip = { "192.168.1.10" }; http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - false, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = false, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": false})"; ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -185,15 +189,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_deny) // NOLINT const sta_ip_string_t remote_ip = { "192.168.1.10" }; http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_deny", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_403, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -237,15 +242,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_basic_success) // NOLINT http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_basic", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -288,15 +294,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_basic_fail_no_header_authorizat http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_basic", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -342,15 +349,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_basic_fail_wrong_header_authori http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_basic", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -396,15 +404,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_basic_fail_short_password) // N http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_basic", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -450,15 +459,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_basic_fail_incorrect_password) http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_basic", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -508,15 +518,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_digest_success) // NOLINT http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_digest", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -561,15 +572,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_digest_fail_no_header_authoriza http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_digest", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -618,15 +630,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_digest_fail_wrong_header_author http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_digest", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -676,15 +689,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_digest_fail_wrong_password) // http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_digest", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -734,15 +748,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_digest_fail_wrong_user) // NOLI http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_digest", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -792,15 +807,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_success) // NOLINT http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -868,15 +884,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_success) // NOLINT http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -920,15 +937,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_success) // NOLINT http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -980,15 +998,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_wrong_password) // N http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1089,15 +1108,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_wrong_user) // NOLIN http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1198,15 +1218,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_empty_user) // NOLIN http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1307,15 +1328,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_wrong_realm) // NOLI http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1416,15 +1438,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_wrong_remote_ip) // http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1525,15 +1548,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_wrong_session_id) // http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1634,15 +1658,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_empty_session_id) // http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1743,15 +1768,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_no_session_id) // NO http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1851,15 +1877,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_bad_body_missing_quo http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -1960,15 +1987,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_bad_body_no_username http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -2068,15 +2096,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_ruuvi_fail_bad_body_no_password http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; const sta_ip_string_t remote_ip = { "192.168.1.10" }; - const http_server_resp_t resp = http_server_handle_req_get_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, - &extra_header_fields); - const string exp_json_resp + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_ruuvi", "lan": true})"; ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); @@ -2169,13 +2198,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_non_empty_success) // NO bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); const string exp_json_resp @@ -2236,13 +2268,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_non_empty_rw_access_to_r bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); const string exp_json_resp @@ -2303,13 +2338,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_non_empty_rw_access_to_r bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = true, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - true, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); const string exp_json_resp @@ -2370,13 +2408,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_non_empty_ro_access_to_r bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = true, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - true, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); const string exp_json_resp @@ -2434,13 +2475,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_non_empty_failed_differe bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); @@ -2495,13 +2539,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_non_empty_failed_wrong_a bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); @@ -2555,13 +2602,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_empty_1) // NOLINT bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); @@ -2614,13 +2664,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_empty_2) // NOLINT bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); @@ -2672,13 +2725,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_no_auth_not_used) // NOL bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); @@ -2731,13 +2787,16 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_bearer_wrong_auth_not_used) // bool flag_access_by_bearer_token = false; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; const http_server_resp_t resp = http_server_handle_req_check_auth( - true, - false, - http_header, - &remote_ip, - &auth_info, - &hostinfo, + ¶m, &extra_header_fields, &flag_access_by_bearer_token); ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); diff --git a/tests/test_http_server_resp/test_http_server_resp.cpp b/tests/test_http_server_resp/test_http_server_resp.cpp index f0dbbc99..7f591c2e 100644 --- a/tests/test_http_server_resp/test_http_server_resp.cpp +++ b/tests/test_http_server_resp/test_http_server_resp.cpp @@ -217,8 +217,7 @@ TEST_F(TestHttpServerResp, resp_data_in_heap_json_with_caching) // NOLINT const bool flag_no_cache = false; const bool flag_add_date = false; - const http_server_resp_t resp = http_server_resp_data_in_heap( - HTTP_RESP_CODE_200, + const http_server_resp_t resp = http_server_resp_200_data_in_heap( HTTP_CONENT_TYPE_APPLICATION_JSON, nullptr, strlen(p_content), @@ -242,8 +241,7 @@ TEST_F(TestHttpServerResp, resp_data_in_heap_json_without_caching) // NOLINT const bool flag_no_cache = true; const bool flag_add_date = false; - const http_server_resp_t resp = http_server_resp_data_in_heap( - HTTP_RESP_CODE_200, + const http_server_resp_t resp = http_server_resp_200_data_in_heap( HTTP_CONENT_TYPE_APPLICATION_JSON, nullptr, strlen(p_content), diff --git a/tests/test_json_access_points/test_json_access_points.cpp b/tests/test_json_access_points/test_json_access_points.cpp index bf0e7d2f..4b000235 100644 --- a/tests/test_json_access_points/test_json_access_points.cpp +++ b/tests/test_json_access_points/test_json_access_points.cpp @@ -23,13 +23,11 @@ class TestJsonAccessPoints : public ::testing::Test void SetUp() override { - json_access_points_init(); } void TearDown() override { - json_access_points_deinit(); } public: