5 const float URL_FH_CURL = -1;
6 const float URL_FH_STDOUT = -2;
15 .url_ready_func url_ready;
16 .entity url_ready_pass;
22 entity url_fromid[NUM_URL_ID];
23 int autocvar__urllib_nextslot;
25 float url_URI_Get_Callback(int id, float status, string data)
27 if (id < MIN_URL_ID) return 0;
29 if (id >= NUM_URL_ID) return 0;
33 if (e.url_rbuf >= 0 || e.url_wbuf >= 0)
35 LOG_INFOF("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url);
39 // whatever happens, we will remove the URL from the list of IDs
40 url_fromid[id] = NULL;
42 // if we get here, we MUST have both buffers cleared
43 if (e.url_rbuf != -1 || e.url_wbuf != -1 || e.url_fh != URL_FH_CURL) error("url_URI_Get_Callback: not a request waiting for data");
49 n = tokenizebyseparator(data, "\n");
50 e.url_rbuf = buf_create();
53 LOG_INFO("url_URI_Get_Callback: out of memory in buf_create\n");
54 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
62 LOG_INFO("url_URI_Get_Callback: out of memory in buf_create\n");
63 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
68 for (i = 0; i < n; ++i)
69 bufstr_set(e.url_rbuf, i, argv(i));
70 e.url_ready(e, e.url_ready_pass, URL_READY_CANREAD);
76 e.url_ready(e, e.url_ready_pass, -fabs(status));
83 void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
87 if (strstrofs(url, "://", 0) >= 0)
93 // collect data to a stringbuffer for a POST request
94 // attempts to close will result in a reading handle
96 // create a writing end that does nothing yet
97 e = new(url_single_fopen_file);
99 e.url_url = strzone(url);
100 e.url_fh = URL_FH_CURL;
101 e.url_wbuf = buf_create();
104 LOG_INFO("url_single_fopen: out of memory in buf_create\n");
105 rdy(e, pass, URL_READY_ERROR);
106 strunzone(e.url_url);
113 e.url_ready_pass = pass;
114 rdy(e, pass, URL_READY_CANWRITE);
120 // get slot for HTTP request
121 for (i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
122 if (url_fromid[i] == NULL) break;
125 for (i = 0; i < autocvar__urllib_nextslot; ++i)
126 if (url_fromid[i] == NULL) break;
127 if (i >= autocvar__urllib_nextslot)
129 LOG_INFO("url_single_fopen: too many concurrent requests\n");
130 rdy(NULL, pass, URL_READY_ERROR);
136 if (!crypto_uri_postbuf(url, i + MIN_URL_ID, string_null, string_null, -1, 0))
138 LOG_INFO("url_single_fopen: failure in crypto_uri_postbuf\n");
139 rdy(NULL, pass, URL_READY_ERROR);
143 // Make a dummy handle object (no buffers at
144 // all). Wait for data to come from the
145 // server, then call the callback
146 e = new(url_single_fopen_file);
148 e.url_url = strzone(url);
149 e.url_fh = URL_FH_CURL;
153 e.url_ready_pass = pass;
157 // make sure this slot won't be reused quickly even on map change
158 cvar_set("_urllib_nextslot", ftos((i + 1) % NUM_URL_ID));
168 e = new(url_single_fopen_stdout);
170 e.url_fh = URL_FH_STDOUT;
172 e.url_ready_pass = pass;
173 rdy(e, pass, URL_READY_CANWRITE);
176 LOG_INFO("url_single_fopen: cannot open '-' for reading\n");
177 rdy(NULL, pass, URL_READY_ERROR);
184 fh = fopen(url, mode);
187 rdy(NULL, pass, URL_READY_ERROR);
192 e = new(url_single_fopen_file);
196 e.url_ready_pass = pass;
197 if (mode == FILE_READ) rdy(e, pass, URL_READY_CANREAD);
198 else rdy(e, pass, URL_READY_CANWRITE);
204 void url_fclose(entity e)
208 if (e.url_fh == URL_FH_CURL)
210 if (e.url_rbuf == -1 || e.url_wbuf != -1) // not(post GET/POST request)
211 if (e.url_rbuf != -1 || e.url_wbuf == -1) // not(pre POST request)
212 error("url_fclose: not closable in current state");
217 // we are closing the write end (HTTP POST request)
219 // get slot for HTTP request
220 for (i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
221 if (url_fromid[i] == NULL) break;
224 for (i = 0; i < autocvar__urllib_nextslot; ++i)
225 if (url_fromid[i] == NULL) break;
226 if (i >= autocvar__urllib_nextslot)
228 LOG_INFO("url_fclose: too many concurrent requests\n");
229 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
231 strunzone(e.url_url);
238 if (!crypto_uri_postbuf(e.url_url, i + MIN_URL_ID, "text/plain", "", e.url_wbuf, 0))
240 LOG_INFO("url_fclose: failure in crypto_uri_postbuf\n");
241 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
243 strunzone(e.url_url);
248 // delete write end. File handle is now in unusable
249 // state. Wait for data to come from the server, then
256 // make sure this slot won't be reused quickly even on map change
257 cvar_set("_urllib_nextslot", ftos((i + 1) % NUM_URL_ID));
261 // we have READ all data, just close
262 e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED);
264 strunzone(e.url_url);
268 else if (e.url_fh == URL_FH_STDOUT)
270 e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED); // closing creates no reading handle
277 e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED); // closing creates no reading handle
282 // with \n (blame FRIK_FILE)
283 string url_fgets(entity e)
285 if (e.url_fh == URL_FH_CURL)
287 if (e.url_rbuf == -1) error("url_fgets: not readable in current state");
290 s = bufstr_get(e.url_rbuf, e.url_rbufpos);
294 else if (e.url_fh == URL_FH_STDOUT)
302 return fgets(e.url_fh);
306 // without \n (blame FRIK_FILE)
307 void url_fputs(entity e, string s)
309 if (e.url_fh == URL_FH_CURL)
311 if (e.url_wbuf == -1) error("url_fputs: not writable in current state");
313 bufstr_set(e.url_wbuf, e.url_wbufpos, s);
316 else if (e.url_fh == URL_FH_STDOUT)
328 // multi URL object, tries URLs separated by space in sequence
329 void url_multi_ready(entity fh, entity me, float status)
332 if (status == URL_READY_ERROR || status < 0)
334 if (status == -422) // Unprocessable Entity
336 LOG_INFO("uri_multi_ready: got HTTP error 422, data is in unusable format - not continuing\n");
337 me.url_ready(fh, me.url_ready_pass, status);
338 strunzone(me.url_url);
343 n = tokenize_console(me.url_url);
344 if (n <= me.url_attempt)
346 me.url_ready(fh, me.url_ready_pass, status);
347 strunzone(me.url_url);
351 url_single_fopen(argv(me.url_attempt), me.url_mode, url_multi_ready, me);
354 me.url_ready(fh, me.url_ready_pass, status);
356 void url_multi_fopen(string url, int mode, url_ready_func rdy, entity pass)
359 n = tokenize_console(url);
362 LOG_INFO("url_multi_fopen: need at least one URL\n");
363 rdy(NULL, pass, URL_READY_ERROR);
367 entity me = new(url_multi);
369 me.url_url = strzone(url);
373 me.url_ready_pass = pass;
374 url_single_fopen(argv(0), mode, url_multi_ready, me);