5 const float URL_FH_CURL = -1;
6 const float URL_FH_STDOUT = -2;
10 .string url_content_type;
17 .url_ready_func url_ready;
18 .entity url_ready_pass;
24 entity url_fromid[NUM_URL_ID];
25 int autocvar__urllib_nextslot;
27 float url_URI_Get_Callback(int id, float status, string data)
29 if (id < MIN_URL_ID) return 0;
31 if (id >= NUM_URL_ID) return 0;
35 if (e.url_rbuf >= 0 || e.url_wbuf >= 0)
37 LOG_INFOF("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url);
41 // whatever happens, we will remove the URL from the list of IDs
42 url_fromid[id] = NULL;
44 // if we get here, we MUST have both buffers cleared
45 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");
51 n = tokenizebyseparator(data, "\n");
52 e.url_rbuf = buf_create();
55 LOG_INFO("url_URI_Get_Callback: out of memory in buf_create\n");
56 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
64 LOG_INFO("url_URI_Get_Callback: out of memory in buf_create\n");
65 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
70 for (i = 0; i < n; ++i)
71 bufstr_set(e.url_rbuf, i, argv(i));
72 e.url_ready(e, e.url_ready_pass, URL_READY_CANREAD);
78 e.url_ready(e, e.url_ready_pass, -fabs(status));
85 void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
89 if (strstrofs(url, "://", 0) >= 0)
95 // collect data to a stringbuffer for a POST request
96 // attempts to close will result in a reading handle
98 // create a writing end that does nothing yet
99 e = new_pure(url_single_fopen_file);
100 e.url_url = strzone(url);
101 e.url_content_type = "text/plain";
103 e.url_fh = URL_FH_CURL;
104 e.url_wbuf = buf_create();
107 LOG_INFO("url_single_fopen: out of memory in buf_create\n");
108 rdy(e, pass, URL_READY_ERROR);
109 strunzone(e.url_url);
116 e.url_ready_pass = pass;
117 rdy(e, pass, URL_READY_CANWRITE);
123 // get slot for HTTP request
124 for (i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
125 if (url_fromid[i] == NULL) break;
128 for (i = 0; i < autocvar__urllib_nextslot; ++i)
129 if (url_fromid[i] == NULL) break;
130 if (i >= autocvar__urllib_nextslot)
132 LOG_INFO("url_single_fopen: too many concurrent requests\n");
133 rdy(NULL, pass, URL_READY_ERROR);
139 if (!crypto_uri_postbuf(url, i + MIN_URL_ID, string_null, string_null, -1, 0))
141 LOG_INFO("url_single_fopen: failure in crypto_uri_postbuf\n");
142 rdy(NULL, pass, URL_READY_ERROR);
146 // Make a dummy handle object (no buffers at
147 // all). Wait for data to come from the
148 // server, then call the callback
149 e = new_pure(url_single_fopen_file);
150 e.url_url = strzone(url);
151 e.url_fh = URL_FH_CURL;
155 e.url_ready_pass = pass;
159 // make sure this slot won't be reused quickly even on map change
160 cvar_set("_urllib_nextslot", ftos((i + 1) % NUM_URL_ID));
170 e = new_pure(url_single_fopen_stdout);
171 e.url_fh = URL_FH_STDOUT;
173 e.url_ready_pass = pass;
174 rdy(e, pass, URL_READY_CANWRITE);
177 LOG_INFO("url_single_fopen: cannot open '-' for reading\n");
178 rdy(NULL, pass, URL_READY_ERROR);
185 fh = fopen(url, mode);
188 rdy(NULL, pass, URL_READY_ERROR);
193 e = new_pure(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, e.url_content_type, e.url_verb, 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_pure(url_multi);
368 me.url_url = strzone(url);
372 me.url_ready_pass = pass;
373 url_single_fopen(argv(0), mode, url_multi_ready, me);