MediaProcessors
uri_parser.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Rafael Antoniello
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
35 #include "uri_parser.h"
36 
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "uriparser/Uri.h"
41 #include "log.h"
42 #include "stat_codes.h"
43 #include "check_utils.h"
44 
45 char* uri_parser_get_uri_part(const char *uri, uri_parser_uri_parts_t part)
46 {
47  UriParserStateA state;
48  UriUriA uri_uri_a;
49  int part_str_size= 0;
50  char *part_str= NULL, *ret_val= NULL; // value to return
51  LOG_CTX_INIT(NULL);
52 
53  /* Check arguments */
54  CHECK_DO(uri!= NULL, return NULL);
55 
56  state.uri = &uri_uri_a;
57  if(uriParseUriA(&state, uri)!= URI_SUCCESS)
58  goto end;
59 
60  switch(part) {
61  case SCHEME:
62  part_str= (char*)uri_uri_a.scheme.first;
63  part_str_size= uri_uri_a.scheme.afterLast- uri_uri_a.scheme.first;
64  break;
65  case HOSTTEXT:
66  part_str= (char*)uri_uri_a.hostText.first;
67  part_str_size= uri_uri_a.hostText.afterLast- uri_uri_a.hostText.first;
68  break;
69  case PORTTEXT:
70  part_str= (char*)uri_uri_a.portText.first;
71  part_str_size= uri_uri_a.portText.afterLast- uri_uri_a.portText.first;
72  break;
73  default:
74  break;
75  }
76 
77  if(part_str!= NULL && part_str_size> 0)
78  ret_val= strndup(part_str, part_str_size);
79 
80 end:
81  uriFreeUriMembersA(&uri_uri_a);
82  return ret_val;
83 }
84 
85 char* uri_parser_query_str_get_value(const char *key, const char *query_str)
86 {
87  UriQueryListA *queryList, *nextNode;
88  int itemCount;
89  size_t query_str_size;
90  char *ret_val= NULL; // value to return, corresponding to 'key' parameter
91  LOG_CTX_INIT(NULL);
92 
93  /* Check arguments */
94  CHECK_DO(key!= NULL, return NULL);
95  CHECK_DO(query_str!= NULL, return NULL);
96 
97  query_str_size= strlen(query_str);
98  if(query_str_size<= 0)
99  return NULL; // nothing to parse
100 
101  if(uriDissectQueryMallocA(&queryList, &itemCount, query_str,
102  &query_str[query_str_size])!= URI_SUCCESS)
103  goto end;
104 
105  nextNode= queryList;
106  while(nextNode!= NULL) {
107  if(strncmp(key, nextNode->key, strlen(key))== 0) {
108  /* Key found! */
109  if(nextNode->value!= NULL)
110  ret_val= strdup(nextNode->value);
111  break;
112  }
113  nextNode= nextNode->next;
114  }
115 
116 end:
117  uriFreeQueryListA(queryList);
118  return ret_val;
119 }
120 
121 int uri_parser_get_id_from_rest_url(const char *url, const char *needle,
122  long long *ref_id)
123 {
124  int end_code= STAT_ENOTFOUND;
125  long long id= -1; // In case of 'NOT FOUND' error
126  char *id_str_start= NULL, *id_str_stop= NULL;
127  LOG_CTX_INIT(NULL);
128 
129  /* Check arguments */
130  CHECK_DO(url!= NULL && strlen(url)> 0, return STAT_ERROR);
131  CHECK_DO(needle!= NULL && strlen(needle)> 0, return STAT_ERROR);
132  CHECK_DO(ref_id!= NULL, return STAT_ERROR);
133 
134  if((id_str_start= strstr(url, needle))!= NULL) {
135  id_str_start+= strlen(needle);
136  id= strtoll(id_str_start, &id_str_stop, 10);
137  //LOGV("'strtoll()' returns: %ll\n", id); // comment-me
138  }
139 
140  /* ID. can be followed by '/' or '.'; the latter refers to string
141  * '.json' that appears at the end of a URL.
142  */
143  if(id_str_stop!= NULL && (*id_str_stop== '/' || *id_str_stop== '.')) {
144  *ref_id= id;
145  end_code= STAT_SUCCESS;
146  } else {
147  *ref_id= -1;
148  end_code= STAT_ENOTFOUND;
149  }
150  return end_code;
151 }
URI parser wrapper.
General status codes enumeration.
#define CHECK_DO(COND, ACTION)
Definition: check_utils.h:57