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