Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers uriqueryparser.c Source File

uriqueryparser.c

00001 /*
00002  * Copyright (c) 2018 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include "assert.h"
00020 #include "mbed-client/uriqueryparser.h"
00021 
00022 // Use int return value instead of ssize_t since we don't have it for all platforms
00023 int parse_query_parameter_value_from_uri(const char *uri, const char *parameter_name, const char **parameter_value)
00024 {
00025     assert(uri);
00026     assert(parameter_name);
00027     assert(parameter_value);
00028 
00029     *parameter_value = NULL;
00030 
00031     char *value_ptr = strchr(uri, '?');
00032     if (value_ptr == NULL) {
00033         return -1;
00034     }
00035 
00036     // Skip '?'
00037     value_ptr++;
00038 
00039     return parse_query_parameter_value_from_query(value_ptr, parameter_name, parameter_value);
00040 }
00041 
00042 // Use int return value instead of ssize_t since we don't have it for all platforms
00043 int parse_query_parameter_value_from_query(const char *query, const char *parameter_name, const char **parameter_value)
00044 {
00045     assert(query);
00046     assert(parameter_name);
00047     assert(parameter_value);
00048 
00049     *parameter_value = NULL;
00050     const int param_name_len = strlen(parameter_name);
00051     const int query_len = strlen(query);
00052 
00053     if (param_name_len == 0 || query_len == 0) {
00054         return -1;
00055     }
00056 
00057     const char *value_ptr = query;
00058 
00059     do {
00060         value_ptr = strstr(value_ptr, parameter_name);
00061 
00062         // No match at all, then break
00063         if (!value_ptr) {
00064             break;
00065         }
00066 
00067         // Check that match was at the beginning or there is a & before the match
00068         if (!(value_ptr == query || *(value_ptr - 1) == '&')) {
00069             // Offset value_ptr past the match to find next one
00070             value_ptr += param_name_len;
00071             continue;
00072         }
00073 
00074         // Offset to after parameter name
00075         value_ptr += param_name_len;
00076 
00077         // Check that parameter was not at the end
00078         if (value_ptr >= query + query_len) {
00079             break;
00080         }
00081 
00082         // CHeck that there is an '=' after parameter name, continue if not as there might be
00083         // another parameter left
00084         if (*value_ptr != '=') {
00085             continue;
00086         }
00087 
00088         *parameter_value = ++value_ptr;
00089         break;
00090 
00091     } while (value_ptr);
00092 
00093     // If parameter found, calculate length of value either ending at an '&' or the end
00094     if (*parameter_value != NULL) {
00095         value_ptr = strchr(*parameter_value, '&');
00096         if (value_ptr) {
00097             return value_ptr - *parameter_value;
00098         }
00099         return strlen(*parameter_value);
00100     }
00101     return -1;
00102 }