| |
1 /* |
| |
2 * (C) Copyright 2008 Wojtek Kaniewski <wojtekka@irc.pl> |
| |
3 * |
| |
4 * This program is free software; you can redistribute it and/or modify |
| |
5 * it under the terms of the GNU Lesser General Public License Version |
| |
6 * 2.1 as published by the Free Software Foundation. |
| |
7 * |
| |
8 * This program is distributed in the hope that it will be useful, |
| |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
11 * GNU Lesser General Public License for more details. |
| |
12 * |
| |
13 * You should have received a copy of the GNU Lesser General Public |
| |
14 * License along with this program; if not, write to the Free Software |
| |
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, |
| |
16 * USA. |
| |
17 */ |
| |
18 |
| |
19 // source: http://toxygen.net/libgadu/ |
| |
20 |
| |
21 #include "oauth-parameter.h" |
| |
22 |
| |
23 struct gg_oauth_parameter { |
| |
24 char *key; |
| |
25 char *value; |
| |
26 struct gg_oauth_parameter *next; |
| |
27 }; |
| |
28 |
| |
29 int gg_oauth_parameter_set(gg_oauth_parameter_t **list, const char *key, const char *value) |
| |
30 { |
| |
31 gg_oauth_parameter_t *p, *new_p; |
| |
32 char *new_key; |
| |
33 char *new_value; |
| |
34 |
| |
35 if (value == NULL) |
| |
36 return 0; |
| |
37 |
| |
38 if (list == NULL) |
| |
39 return -1; |
| |
40 |
| |
41 new_key = strdup(key); |
| |
42 |
| |
43 if (new_key == NULL) |
| |
44 return -1; |
| |
45 |
| |
46 new_value = strdup(value); |
| |
47 |
| |
48 if (new_value == NULL) { |
| |
49 free(new_key); |
| |
50 return -1; |
| |
51 } |
| |
52 |
| |
53 new_p = malloc(sizeof(gg_oauth_parameter_t)); |
| |
54 |
| |
55 if (new_p == NULL) { |
| |
56 free(new_key); |
| |
57 free(new_value); |
| |
58 return -1; |
| |
59 } |
| |
60 |
| |
61 memset(new_p, 0, sizeof(gg_oauth_parameter_t)); |
| |
62 new_p->key = new_key; |
| |
63 new_p->value = new_value; |
| |
64 |
| |
65 if (*list != NULL) { |
| |
66 p = *list; |
| |
67 |
| |
68 while (p != NULL && p->next != NULL) |
| |
69 p = p->next; |
| |
70 |
| |
71 p->next = new_p; |
| |
72 } else { |
| |
73 *list = new_p; |
| |
74 } |
| |
75 |
| |
76 return 0; |
| |
77 } |
| |
78 |
| |
79 char *gg_oauth_parameter_join(gg_oauth_parameter_t *list, int header) |
| |
80 { |
| |
81 gg_oauth_parameter_t *p; |
| |
82 int len = 0; |
| |
83 char *res, *out; |
| |
84 |
| |
85 if (header) |
| |
86 len += strlen("Authorization: OAuth "); |
| |
87 |
| |
88 for (p = list; p; p = p->next) { |
| |
89 gchar *escaped; |
| |
90 len += strlen(p->key); |
| |
91 |
| |
92 len += (header) ? 3 : 1; |
| |
93 |
| |
94 escaped = g_uri_escape_string(p->value, NULL, FALSE); |
| |
95 len += strlen(escaped); |
| |
96 g_free(escaped); |
| |
97 |
| |
98 if (p->next) |
| |
99 len += 1; |
| |
100 } |
| |
101 |
| |
102 res = malloc(len + 1); |
| |
103 |
| |
104 if (res == NULL) |
| |
105 return NULL; |
| |
106 |
| |
107 out = res; |
| |
108 |
| |
109 *out = 0; |
| |
110 |
| |
111 if (header) { |
| |
112 strcpy(out, "Authorization: OAuth "); |
| |
113 out += strlen(out); |
| |
114 } |
| |
115 |
| |
116 for (p = list; p; p = p->next) { |
| |
117 gchar *escaped; |
| |
118 strcpy(out, p->key); |
| |
119 out += strlen(p->key); |
| |
120 |
| |
121 strcpy(out++, "="); |
| |
122 |
| |
123 if (header) |
| |
124 strcpy(out++, "\""); |
| |
125 |
| |
126 escaped = g_uri_escape_string(p->value, NULL, FALSE); |
| |
127 strcpy(out, escaped); |
| |
128 out += strlen(escaped); |
| |
129 g_free(escaped); |
| |
130 |
| |
131 if (header) |
| |
132 strcpy(out++, "\""); |
| |
133 |
| |
134 if (p->next != NULL) |
| |
135 strcpy(out++, (header) ? "," : "&"); |
| |
136 } |
| |
137 |
| |
138 return res; |
| |
139 } |
| |
140 |
| |
141 void gg_oauth_parameter_free(gg_oauth_parameter_t *list) |
| |
142 { |
| |
143 while (list != NULL) { |
| |
144 gg_oauth_parameter_t *next; |
| |
145 |
| |
146 next = list->next; |
| |
147 |
| |
148 free(list->key); |
| |
149 free(list->value); |
| |
150 free(list); |
| |
151 |
| |
152 list = next; |
| |
153 } |
| |
154 } |