leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2018 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16
leothedragon 0:8f0bb79ddd48 17 #include <stdlib.h>
leothedragon 0:8f0bb79ddd48 18 #include <string.h>
leothedragon 0:8f0bb79ddd48 19 #include "assert.h"
leothedragon 0:8f0bb79ddd48 20 #include "mbed-client/uriqueryparser.h"
leothedragon 0:8f0bb79ddd48 21
leothedragon 0:8f0bb79ddd48 22 // Use int return value instead of ssize_t since we don't have it for all platforms
leothedragon 0:8f0bb79ddd48 23 int parse_query_parameter_value_from_uri(const char *uri, const char *parameter_name, const char **parameter_value)
leothedragon 0:8f0bb79ddd48 24 {
leothedragon 0:8f0bb79ddd48 25 assert(uri);
leothedragon 0:8f0bb79ddd48 26 assert(parameter_name);
leothedragon 0:8f0bb79ddd48 27 assert(parameter_value);
leothedragon 0:8f0bb79ddd48 28
leothedragon 0:8f0bb79ddd48 29 *parameter_value = NULL;
leothedragon 0:8f0bb79ddd48 30
leothedragon 0:8f0bb79ddd48 31 char *value_ptr = strchr(uri, '?');
leothedragon 0:8f0bb79ddd48 32 if (value_ptr == NULL) {
leothedragon 0:8f0bb79ddd48 33 return -1;
leothedragon 0:8f0bb79ddd48 34 }
leothedragon 0:8f0bb79ddd48 35
leothedragon 0:8f0bb79ddd48 36 // Skip '?'
leothedragon 0:8f0bb79ddd48 37 value_ptr++;
leothedragon 0:8f0bb79ddd48 38
leothedragon 0:8f0bb79ddd48 39 return parse_query_parameter_value_from_query(value_ptr, parameter_name, parameter_value);
leothedragon 0:8f0bb79ddd48 40 }
leothedragon 0:8f0bb79ddd48 41
leothedragon 0:8f0bb79ddd48 42 // Use int return value instead of ssize_t since we don't have it for all platforms
leothedragon 0:8f0bb79ddd48 43 int parse_query_parameter_value_from_query(const char *query, const char *parameter_name, const char **parameter_value)
leothedragon 0:8f0bb79ddd48 44 {
leothedragon 0:8f0bb79ddd48 45 assert(query);
leothedragon 0:8f0bb79ddd48 46 assert(parameter_name);
leothedragon 0:8f0bb79ddd48 47 assert(parameter_value);
leothedragon 0:8f0bb79ddd48 48
leothedragon 0:8f0bb79ddd48 49 *parameter_value = NULL;
leothedragon 0:8f0bb79ddd48 50 const int param_name_len = strlen(parameter_name);
leothedragon 0:8f0bb79ddd48 51 const int query_len = strlen(query);
leothedragon 0:8f0bb79ddd48 52
leothedragon 0:8f0bb79ddd48 53 if (param_name_len == 0 || query_len == 0) {
leothedragon 0:8f0bb79ddd48 54 return -1;
leothedragon 0:8f0bb79ddd48 55 }
leothedragon 0:8f0bb79ddd48 56
leothedragon 0:8f0bb79ddd48 57 const char *value_ptr = query;
leothedragon 0:8f0bb79ddd48 58
leothedragon 0:8f0bb79ddd48 59 do {
leothedragon 0:8f0bb79ddd48 60 value_ptr = strstr(value_ptr, parameter_name);
leothedragon 0:8f0bb79ddd48 61
leothedragon 0:8f0bb79ddd48 62 // No match at all, then break
leothedragon 0:8f0bb79ddd48 63 if (!value_ptr) {
leothedragon 0:8f0bb79ddd48 64 break;
leothedragon 0:8f0bb79ddd48 65 }
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 // Check that match was at the beginning or there is a & before the match
leothedragon 0:8f0bb79ddd48 68 if (!(value_ptr == query || *(value_ptr - 1) == '&')) {
leothedragon 0:8f0bb79ddd48 69 // Offset value_ptr past the match to find next one
leothedragon 0:8f0bb79ddd48 70 value_ptr += param_name_len;
leothedragon 0:8f0bb79ddd48 71 continue;
leothedragon 0:8f0bb79ddd48 72 }
leothedragon 0:8f0bb79ddd48 73
leothedragon 0:8f0bb79ddd48 74 // Offset to after parameter name
leothedragon 0:8f0bb79ddd48 75 value_ptr += param_name_len;
leothedragon 0:8f0bb79ddd48 76
leothedragon 0:8f0bb79ddd48 77 // Check that parameter was not at the end
leothedragon 0:8f0bb79ddd48 78 if (value_ptr >= query + query_len) {
leothedragon 0:8f0bb79ddd48 79 break;
leothedragon 0:8f0bb79ddd48 80 }
leothedragon 0:8f0bb79ddd48 81
leothedragon 0:8f0bb79ddd48 82 // CHeck that there is an '=' after parameter name, continue if not as there might be
leothedragon 0:8f0bb79ddd48 83 // another parameter left
leothedragon 0:8f0bb79ddd48 84 if (*value_ptr != '=') {
leothedragon 0:8f0bb79ddd48 85 continue;
leothedragon 0:8f0bb79ddd48 86 }
leothedragon 0:8f0bb79ddd48 87
leothedragon 0:8f0bb79ddd48 88 *parameter_value = ++value_ptr;
leothedragon 0:8f0bb79ddd48 89 break;
leothedragon 0:8f0bb79ddd48 90
leothedragon 0:8f0bb79ddd48 91 } while (value_ptr);
leothedragon 0:8f0bb79ddd48 92
leothedragon 0:8f0bb79ddd48 93 // If parameter found, calculate length of value either ending at an '&' or the end
leothedragon 0:8f0bb79ddd48 94 if (*parameter_value != NULL) {
leothedragon 0:8f0bb79ddd48 95 value_ptr = strchr(*parameter_value, '&');
leothedragon 0:8f0bb79ddd48 96 if (value_ptr) {
leothedragon 0:8f0bb79ddd48 97 return value_ptr - *parameter_value;
leothedragon 0:8f0bb79ddd48 98 }
leothedragon 0:8f0bb79ddd48 99 return strlen(*parameter_value);
leothedragon 0:8f0bb79ddd48 100 }
leothedragon 0:8f0bb79ddd48 101 return -1;
leothedragon 0:8f0bb79ddd48 102 }