1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "oauth2_util.h"
int
oauth2_util_get_query(const char *url, char **query)
{
OAUTH2_RETURN_VAL(url, {}, OAUTH2_ERROR_INVALID_PARAMETER,
"url is Null");
OAUTH2_RETURN_VAL(query, {}, OAUTH2_ERROR_INVALID_PARAMETER,
"query is Null");
int query_len = 0;
char *part = NULL;
char *query_end = NULL;
char *query_start = strchr(url, '?');
if (query_start) {
query_start++;
query_end = query_start;
while (*query_end != '\0' && *query_end != '#')
query_end++;
query_len = query_end - query_start;
part = (char *) malloc((query_len + 1) * sizeof(char));
OAUTH2_RETURN_VAL(part, {}, OAUTH2_ERROR_OUT_OF_MEMORY,
"Out of memory");
memset(part, '\0', (query_len + 1) * sizeof(char));
strncpy(part, query_start, query_len);
part[query_len] = '\0';
}
*query = part;
return OAUTH2_ERROR_NONE;
}
int
oauth2_util_get_fragment(const char *url, char **fragment)
{
OAUTH2_RETURN_VAL(url, {}, OAUTH2_ERROR_INVALID_PARAMETER,
"url is Null");
OAUTH2_RETURN_VAL(fragment, {}, OAUTH2_ERROR_INVALID_PARAMETER,
"fragment is Null");
int fragment_len = 0;
char *part = NULL;
char *fragment_end = NULL;
char *fragment_start = strchr(url, '#');
if (fragment_start) {
fragment_start++;
fragment_end = fragment_start;
while (*fragment_end != '\0')
fragment_end++;
fragment_len = fragment_end - fragment_start;
part = (char *) malloc((fragment_len + 1) * sizeof(char));
OAUTH2_RETURN_VAL(part, {}, OAUTH2_ERROR_OUT_OF_MEMORY,
"Out of memory");
memset(part, '\0', (fragment_len + 1) * sizeof(char));
strncpy(part, fragment_start, fragment_len);
part[fragment_len] = '\0';
}
*fragment = part;
return OAUTH2_ERROR_NONE;
}
int
oauth2_util_get_params(const char *url_part, bundle **params)
{
OAUTH2_RETURN_VAL(url_part, {}, OAUTH2_ERROR_INVALID_PARAMETER,
"url part is Null");
OAUTH2_RETURN_VAL(params, {}, OAUTH2_ERROR_INVALID_PARAMETER,
"params is Null");
char *url = (char *) url_part;
int idx = 0;
gchar **pch = g_strsplit_set(url, "&;", -1);
while (pch && pch[idx] && strlen(pch[idx])) {
char *tmp_start = pch[idx];
char *tmp_end = tmp_start;
while (*tmp_end != '\0')
tmp_end++;
char *eq_ptr = strchr(tmp_start, '=');
int key_size = (eq_ptr - tmp_start) + 1;
char *key = (char *) malloc((key_size) * sizeof(char));
if (key == NULL) {
OAUTH2_LOG_E("Out of memory");
return OAUTH2_ERROR_OUT_OF_MEMORY;
}
memset(key, '\0', ((eq_ptr - tmp_start) + 1) * sizeof(char));
strncpy(key, tmp_start, (eq_ptr - tmp_start));
if (eq_ptr != NULL)
eq_ptr++;
key_size = (tmp_end - eq_ptr) + 1;
char *val = (char *) malloc((key_size) * sizeof(char));
if (val == NULL) {
OAUTH2_LOG_E("Out of memory");
OAUTH2_FREE(key);
return OAUTH2_ERROR_OUT_OF_MEMORY;
}
memset(val, '\0', ((tmp_end - eq_ptr) + 1) * sizeof(char));
strncpy(val, eq_ptr, (tmp_end - eq_ptr));
bundle_add_str(*params, key, val);
idx++;
OAUTH2_FREE(key);
OAUTH2_FREE(val);
}
return OAUTH2_ERROR_NONE;
}
|