Skip to content
Newer
Older
100644 229 lines (204 sloc) 6.57 KB
5a9caf9 @eighthave update Makefile and arrange files to match the Library Template:
eighthave authored Jan 28, 2012
1 #include "purest_json.h"
2
3 static t_class *rest_class;
4
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
5 static size_t write_memory_callback(void *ptr, size_t size, size_t nmemb, void *data) {
6 size_t realsize = size * nmemb;
7 t_memory_struct *mem = (t_memory_struct *)data;
8
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
9 mem->memory = (char *) resizebytes(mem->memory, mem->size, mem->size + realsize + 1);
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
10 if (mem->memory == NULL) {
11 /* out of memory! */
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
12 error("not enough memory");
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
13 }
14 memcpy(&mem->memory[mem->size], ptr, realsize);
15 if (mem->size + realsize - mem->size != realsize) {
16 error("Integer overflow or similar. Bad Things can happen.");
17 }
18 mem->size += realsize;
19 mem->memory[mem->size] = '\0';
20
21 return realsize;
22 }
23
24 static size_t read_memory_callback(void *ptr, size_t size, size_t nmemb, void *data) {
25 size_t realsize = size * nmemb;
26 t_memory_struct *mem = (t_memory_struct *)data;
27 size_t to_copy = (mem->size < realsize) ? mem->size : realsize;
28
29 memcpy(ptr, mem->memory, to_copy);
30 mem->size -= to_copy;
31 mem->memory += to_copy;
32 return to_copy;
33 }
34
5a9caf9 @eighthave update Makefile and arrange files to match the Library Template:
eighthave authored Jan 29, 2012
35 void setup_rest0x2djson(void) {
4596bdf @residuum Renamed library to purest_json;
authored Sep 12, 2011
36 rest_class = class_new(gensym("rest-json"), (t_newmethod)rest_new,
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
37 (t_method)rest_free, sizeof(t_rest), 0, A_GIMME, 0);
4596bdf @residuum Renamed library to purest_json;
authored Sep 12, 2011
38 class_addmethod(rest_class, (t_method)rest_oauth, gensym("oauth"), A_GIMME, 0);
39 class_addmethod(rest_class, (t_method)rest_url, gensym("url"), A_GIMME, 0);
40 class_addmethod(rest_class, (t_method)rest_command, gensym("PUT"), A_GIMME, 0);
41 class_addmethod(rest_class, (t_method)rest_command, gensym("GET"), A_GIMME, 0);
42 class_addmethod(rest_class, (t_method)rest_command, gensym("DELETE"), A_GIMME, 0);
43 class_addmethod(rest_class, (t_method)rest_command, gensym("POST"), A_GIMME, 0);
631941d @residuum Setup methods in their respective files.
authored Aug 23, 2011
44 }
45
4596bdf @residuum Renamed library to purest_json;
authored Sep 12, 2011
46 void rest_command(t_rest *x, t_symbol *selector, int argcount, t_atom *argvec) {
84f05ed @residuum Removed unnecessary selector from CouchDB message.
authored Sep 3, 2011
47 char *request_type;
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
48 char path[MAXPDSTRING];
49 char parameters[MAXPDSTRING];
50 char *cleaned_parameters;
51 size_t memsize = 0;
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
52 if(x->is_data_locked) {
53 post("rest-json object is performing request and locked");
54 } else {
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
55 memset(x->request_type, 0x00, 7);
56 memset(x->parameters, 0x00, MAXPDSTRING);
57 memset(x->complete_url, 0x00, MAXPDSTRING);
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
58 switch (argcount) {
59 case 0:
60 break;
61 default:
62 request_type = selector->s_name;
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
63 atom_string(argvec, path, MAXPDSTRING);
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
64 if (argcount > 1) {
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
65 atom_string(argvec + 1, parameters, MAXPDSTRING);
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
66 }
67 x->is_data_locked = 1;
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
68 if (x->base_url != NULL) {
69 strcpy(x->complete_url, x->base_url);
70 }
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
71 strcat(x->complete_url, path);
72 strcpy(x->request_type, request_type);
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
73 if (parameters != NULL) {
74 cleaned_parameters = remove_backslashes(parameters, memsize);
75 strcpy(x->parameters, cleaned_parameters);
76 freebytes(cleaned_parameters, memsize);
77 }
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
78 execute_rest(x);
79 break;
80 }
cd8c974 @residuum First commit.
authored Aug 20, 2011
81 }
82 }
83
4596bdf @residuum Renamed library to purest_json;
authored Sep 12, 2011
84 void rest_oauth(t_rest *x, t_symbol *selector, int argcount, t_atom *argvec) {
cd8c974 @residuum First commit.
authored Aug 20, 2011
85 error("OAUTH not implemented yet.");
68efd0b @residuum Do not show unused parameter warning.
authored Feb 8, 2012
86
87 (void) x;
88 (void) selector;
89 (void) argcount;
90 (void) argvec;
cd8c974 @residuum First commit.
authored Aug 20, 2011
91 }
92
4596bdf @residuum Renamed library to purest_json;
authored Sep 12, 2011
93 void rest_url(t_rest *x, t_symbol *selector, int argcount, t_atom *argvec) {
68efd0b @residuum Do not show unused parameter warning.
authored Feb 7, 2012
94
95 (void) selector;
96
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
97 if(x->is_data_locked) {
98 post("rest-json object is performing request and locked");
99 } else {
100 switch (argcount) {
101 case 1:
102 if (argvec[0].a_type != A_SYMBOL) {
103 error("Base URL cannot be set.");
104 } else {
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
105 if (x->base_url == NULL) {
106 x->base_url = (char *)getbytes(MAXPDSTRING * sizeof(char));
107 }
108 atom_string(argvec, x->base_url, MAXPDSTRING);
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
109 }
110 break;
111 case 0:
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
112 if (x->base_url != NULL) {
113 freebytes(x->base_url, MAXPDSTRING * sizeof(char));
114 }
115 x->base_url = NULL;
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
116 break;
117 default:
118 error("Too many parameters.");
119 break;
120 }
cd8c974 @residuum First commit.
authored Aug 20, 2011
121 }
122 }
123
4596bdf @residuum Renamed library to purest_json;
authored Sep 12, 2011
124 void *rest_new(t_symbol *selector, int argcount, t_atom *argvec) {
125 t_rest *x = (t_rest *)pd_new(rest_class);
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
126
68efd0b @residuum Do not show unused parameter warning.
authored Feb 7, 2012
127 (void) selector;
128
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
129 switch (argcount) {
cd8c974 @residuum First commit.
authored Aug 20, 2011
130 case 1:
131 if (argvec[0].a_type != A_SYMBOL) {
91985ed @residuum Improved threading:
authored Nov 7, 2011
132 error("Base URL cannot be set.");
cd8c974 @residuum First commit.
authored Aug 20, 2011
133 } else {
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
134 x->base_url = (char *)getbytes(MAXPDSTRING * sizeof(char));
135 atom_string(argvec, x->base_url, MAXPDSTRING);
cd8c974 @residuum First commit.
authored Aug 20, 2011
136 }
137 break;
138 case 0:
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
139 x->base_url = NULL;
cd8c974 @residuum First commit.
authored Aug 20, 2011
140 break;
141 default:
142 error("Too many parameters.");
143 break;
144 }
76dfd69 @residuum JSON decoding working.
authored Aug 25, 2011
145 outlet_new(&x->x_ob, NULL);
7cfd381 @residuum Output JSON data as several lists, followed by a bang.
authored Aug 31, 2011
146 x->done_outlet = outlet_new(&x->x_ob, &s_bang);
91985ed @residuum Improved threading:
authored Nov 7, 2011
147 x->is_data_locked = 0;
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
148
cd8c974 @residuum First commit.
authored Aug 20, 2011
149 return (void *)x;
150 }
151
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
152 void rest_free (t_rest *x, t_symbol *selector, int argcount, t_atom *argvec) {
153 (void) selector;
154 (void) argcount;
155 (void) argvec;
156
157 if (x->base_url != NULL) {
158 freebytes(x->base_url, MAXPDSTRING * sizeof(char));
159 }
160 }
161
fc6e108 @residuum Removed blocking via pthread.
authored Sep 16, 2011
162 void *execute_rest_thread(void *thread_args) {
91985ed @residuum Improved threading:
authored Nov 7, 2011
163 t_rest *x = (t_rest *)thread_args;
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
164 CURL *curl_handle;
165 CURLcode result;
03793eb @residuum Puttin data into CouchDB possible.
authored Aug 24, 2011
166 t_memory_struct in_memory;
167 t_memory_struct out_memory;
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
168
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
169 curl_global_init(CURL_GLOBAL_ALL);
170 curl_handle = curl_easy_init();
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
171 post("url: %s", x->complete_url);
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
172 if (curl_handle) {
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
173 curl_easy_setopt(curl_handle, CURLOPT_URL, x->complete_url);
4782903 @residuum Fixed threading issues with libcurl - hopefully.
authored Nov 18, 2011
174 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
175 if (strcmp(x->request_type, "PUT") == 0) {
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
176 curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, TRUE);
03793eb @residuum Puttin data into CouchDB possible.
authored Aug 24, 2011
177 curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_memory_callback);
178 /* Prepare data for reading */
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
179 in_memory.memory = getbytes(strlen(x->parameters) + 1);
180 if (in_memory.memory == NULL) {
181 error("not enough memory");
182 }
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
183 in_memory.size = strlen(x->parameters);
184 memcpy(in_memory.memory, x->parameters, strlen(x->parameters));
03793eb @residuum Puttin data into CouchDB possible.
authored Aug 24, 2011
185 curl_easy_setopt(curl_handle, CURLOPT_READDATA, (void *)&in_memory);
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
186 } else if (strcmp(x->request_type, "POST") == 0) {
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
187 curl_easy_setopt(curl_handle, CURLOPT_POST, TRUE);
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
188 } else if (strcmp(x->request_type, "DELETE") == 0) {
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
189 curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
190 }
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
191 out_memory.memory = getbytes(1);
03793eb @residuum Puttin data into CouchDB possible.
authored Aug 24, 2011
192 out_memory.size = 0;
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
193 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_memory_callback);
03793eb @residuum Puttin data into CouchDB possible.
authored Aug 24, 2011
194 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&out_memory);
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
195 result = curl_easy_perform(curl_handle);
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
196
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
197 if (result == CURLE_OK) {
5f000f9 @residuum Fix for issue #5: Unicode characters are converted to lowercase.
authored Feb 4, 2012
198 output_json_string(out_memory.memory, x->x_ob.ob_outlet, x->done_outlet);
76dfd69 @residuum JSON decoding working.
authored Aug 25, 2011
199 /* Free memory */
03793eb @residuum Puttin data into CouchDB possible.
authored Aug 24, 2011
200 if (out_memory.memory) {
5854491 @residuum rest-json is now working again.
authored Mar 26, 2012
201 freebytes(out_memory.memory, out_memory.size + 1);
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
202 }
9b28222 @residuum Some small bug fixes and cleanups.
authored Feb 10, 2012
203 free((void *)result);
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
204 } else {
205 error("Error while performing request: %s", curl_easy_strerror(result));
206 }
207 curl_easy_cleanup(curl_handle);
208 } else {
209 error("Cannot init curl.");
210 }
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
211 x->is_data_locked = 0;
fc6e108 @residuum Removed blocking via pthread.
authored Sep 16, 2011
212 pthread_exit(NULL);
213 }
214
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
215 void execute_rest(t_rest *x) {
fc6e108 @residuum Removed blocking via pthread.
authored Sep 16, 2011
216 int rc;
217 pthread_t thread;
91985ed @residuum Improved threading:
authored Nov 7, 2011
218 pthread_attr_t thread_attributes;
31bd302 @residuum Fixed threading issues, refactoring of methods
authored Nov 15, 2011
219
91985ed @residuum Improved threading:
authored Nov 7, 2011
220 pthread_attr_init(&thread_attributes);
221 pthread_attr_setdetachstate(&thread_attributes, PTHREAD_CREATE_DETACHED);
8c29f73 @residuum Locking rest-json
authored Nov 22, 2011
222 rc = pthread_create(&thread, &thread_attributes, execute_rest_thread, (void *)x);
223 pthread_attr_destroy(&thread_attributes);
224 if (rc) {
225 error("Could not create thread with code %d", rc);
226 x->is_data_locked = 0;
fc6e108 @residuum Removed blocking via pthread.
authored Sep 16, 2011
227 }
7e0f670 @residuum Basic couchdb action is possible.
authored Aug 21, 2011
228 }
Something went wrong with that request. Please try again.