Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
urllib.qc File Reference
#include "urllib.qh"
Include dependency graph for urllib.qc:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ERASEABLE void url_fclose (entity e)
ERASEABLE string url_fgets (entity e)
ERASEABLE void url_fputs (entity e, string s)
ERASEABLE void url_multi_fopen (string url, int mode, url_ready_func rdy, entity pass)
ERASEABLE void url_multi_ready (entity fh, entity me, float status)
ERASEABLE void url_single_fopen (string url, int mode, url_ready_func rdy, entity pass)
ERASEABLE float url_URI_Get_Callback (int id, float status, string data)

Variables

int autocvar__urllib_nextslot
int url_attempt
string url_content_type
float url_fh
const float URL_FH_CURL = -1
const float URL_FH_STDOUT = -2
entity url_fromid [NUM_URL_ID]
float url_id
int url_mode
float url_rbuf
float url_rbufpos
url_ready_func url_ready
entity url_ready_pass
string url_url
string url_verb
float url_wbuf
float url_wbufpos

Function Documentation

◆ url_fclose()

ERASEABLE void url_fclose ( entity e)

Definition at line 205 of file urllib.qc.

206{
207 int i;
208
209 if (e.url_fh == URL_FH_CURL)
210 {
211 if (e.url_rbuf == -1 || e.url_wbuf != -1) // not(post GET/POST request)
212 if (e.url_rbuf != -1 || e.url_wbuf == -1) // not(pre POST request)
213 error("url_fclose: not closable in current state");
214
215 // closing an URL!
216 if (e.url_wbuf >= 0)
217 {
218 // we are closing the write end (HTTP POST request)
219
220 // get slot for HTTP request
221 for (i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
222 if (url_fromid[i] == NULL) break;
223 if (i >= NUM_URL_ID)
224 {
225 for (i = 0; i < autocvar__urllib_nextslot; ++i)
226 if (url_fromid[i] == NULL) break;
228 {
229 LOG_INFO("url_fclose: too many concurrent requests");
230 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
231 buf_del(e.url_wbuf);
232 strfree(e.url_url);
233 delete(e);
234 return;
235 }
236 }
237
238 // POST the data
239 if (!crypto_uri_postbuf(e.url_url, i + MIN_URL_ID, e.url_content_type, e.url_verb, e.url_wbuf, 0))
240 {
241 LOG_INFO("url_fclose: failure in crypto_uri_postbuf");
242 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
243 buf_del(e.url_wbuf);
244 strfree(e.url_url);
245 delete(e);
246 return;
247 }
248
249 // delete write end. File handle is now in unusable
250 // state. Wait for data to come from the server, then
251 // call the callback
252 buf_del(e.url_wbuf);
253 e.url_wbuf = -1;
254 e.url_id = i;
255 url_fromid[i] = e;
256
257 // make sure this slot won't be reused quickly even on map change
258 cvar_set("_urllib_nextslot", ftos((i + 1) % NUM_URL_ID));
259 }
260 else
261 {
262 // we have READ all data, just close
263 e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED);
264 buf_del(e.url_rbuf);
265 strfree(e.url_url);
266 delete(e);
267 }
268 }
269 else if (e.url_fh == URL_FH_STDOUT)
270 {
271 e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED); // closing creates no reading handle
272 delete(e);
273 }
274 else
275 {
276 // file
277 fclose(e.url_fh);
278 e.url_ready(e, e.url_ready_pass, URL_READY_CLOSED); // closing creates no reading handle
279 delete(e);
280 }
281}
#define LOG_INFO(...)
Definition log.qh:65
void cvar_set(string name, string value)
void fclose(float fhandle)
string ftos(float f)
#define NULL
Definition post.qh:14
#define error
Definition pre.qh:6
#define strfree(this)
Definition string.qh:59
int autocvar__urllib_nextslot
Definition urllib.qc:25
const float URL_FH_CURL
Definition urllib.qc:5
entity url_fromid[NUM_URL_ID]
Definition urllib.qc:24
const float URL_FH_STDOUT
Definition urllib.qc:6
const float URL_READY_CLOSED
Definition urllib.qh:15
const float URL_READY_ERROR
Definition urllib.qh:14
#define MIN_URL_ID
Definition urllib.qh:33
#define NUM_URL_ID
Definition urllib.qh:34

References autocvar__urllib_nextslot, cvar_set(), entity(), error, fclose(), ftos(), LOG_INFO, MIN_URL_ID, NULL, NUM_URL_ID, strfree, URL_FH_CURL, URL_FH_STDOUT, url_fromid, URL_READY_CLOSED, and URL_READY_ERROR.

Referenced by AdditionalServerInfo_OnGet(), MX_JLF_(), MX_Messages_(), MX_Nick_(), MX_Say_(), MX_Sync_(), MX_Typing_(), PlayerStats_GameReport_Handler(), PlayerStats_PlayerBasic_Handler(), PlayerStats_PlayerDetail_Handler(), WeaponStats_ready(), and XonoticToS_OnGet().

◆ url_fgets()

ERASEABLE string url_fgets ( entity e)

Definition at line 285 of file urllib.qc.

286{
287 if (e.url_fh == URL_FH_CURL)
288 {
289 if (e.url_rbuf == -1) error("url_fgets: not readable in current state");
290 // curl
291 string s = bufstr_get(e.url_rbuf, e.url_rbufpos);
292 ++e.url_rbufpos;
293 return s;
294 }
295 else if (e.url_fh == URL_FH_STDOUT)
296 {
297 // stdout
298 return string_null;
299 }
300 else
301 {
302 // file
303 return fgets(e.url_fh);
304 }
305}
string fgets(float fhandle)
string string_null
Definition nil.qh:9

References entity(), error, fgets(), string_null, URL_FH_CURL, and URL_FH_STDOUT.

Referenced by AdditionalServerInfo_OnGet(), MX_Messages_(), MX_Sync_(), PlayerStats_GameReport_Handler(), PlayerStats_PlayerBasic_Handler(), PlayerStats_PlayerDetail_Handler(), WeaponStats_ready(), and XonoticToS_OnGet().

◆ url_fputs()

ERASEABLE void url_fputs ( entity e,
string s )

Definition at line 309 of file urllib.qc.

310{
311 if (e.url_fh == URL_FH_CURL)
312 {
313 if (e.url_wbuf == -1) error("url_fputs: not writable in current state");
314 // curl
315 bufstr_set(e.url_wbuf, e.url_wbufpos, s);
316 ++e.url_wbufpos;
317 }
318 else if (e.url_fh == URL_FH_STDOUT)
319 {
320 // stdout
321 print(s);
322 }
323 else
324 {
325 // file
326 fputs(e.url_fh, s);
327 }
328}
void fputs(float fhandle, string s)
void print(string text,...)

References entity(), error, fputs(), print(), URL_FH_CURL, and URL_FH_STDOUT.

Referenced by MX_JLF_(), MX_Nick_(), MX_Say_(), MX_Typing_(), PlayerStats_GameReport_Handler(), PlayerStats_PlayerBasic_Handler(), PlayerStats_PlayerDetail_Handler(), and WeaponStats_ready().

◆ url_multi_fopen()

ERASEABLE void url_multi_fopen ( string url,
int mode,
url_ready_func rdy,
entity pass )

Definition at line 361 of file urllib.qc.

362{
363 float n = tokenize_console(url);
364 if (n <= 0)
365 {
366 LOG_INFO("url_multi_fopen: need at least one URL");
368 return;
369 }
370
371 entity me = new_pure(url_multi);
372 me.url_url = strzone(url);
373 me.url_attempt = 0;
374 me.url_mode = mode;
375 me.url_ready = rdy;
376 me.url_ready_pass = pass;
378}
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
#define tokenize_console
#define pass(name, colormin, colormax)
string strzone(string s)
string argv(float n)
#define new_pure(class)
purely logical entities (not linked to the area grid)
Definition oo.qh:67
ERASEABLE void url_multi_ready(entity fh, entity me, float status)
Definition urllib.qc:332
ERASEABLE void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
Definition urllib.qc:86

References argv(), entity(), LOG_INFO, new_pure, NULL, pass, strzone(), tokenize_console, url_multi_ready(), URL_READY_ERROR, and url_single_fopen().

Referenced by PlayerStats_GameReport(), and WeaponStats_Shutdown().

◆ url_multi_ready()

ERASEABLE void url_multi_ready ( entity fh,
entity me,
float status )

Definition at line 332 of file urllib.qc.

333{
334 float n;
335 if (status == URL_READY_ERROR || status < 0)
336 {
337 if (status == -422) // Unprocessable Entity
338 {
339 LOG_INFO("uri_multi_ready: got HTTP error 422, data is in unusable format - not continuing");
340 me.url_ready(fh, me.url_ready_pass, status);
341 strfree(me.url_url);
342 delete(me);
343 return;
344 }
345 ++me.url_attempt;
346 n = tokenize_console(me.url_url);
347 if (n <= me.url_attempt)
348 {
349 me.url_ready(fh, me.url_ready_pass, status);
350 strfree(me.url_url);
351 delete(me);
352 return;
353 }
354 url_single_fopen(argv(me.url_attempt), me.url_mode, url_multi_ready, me);
355 return;
356 }
357 me.url_ready(fh, me.url_ready_pass, status);
358}

References argv(), entity(), LOG_INFO, strfree, tokenize_console, url_multi_ready(), URL_READY_ERROR, and url_single_fopen().

Referenced by url_multi_fopen(), and url_multi_ready().

◆ url_single_fopen()

ERASEABLE void url_single_fopen ( string url,
int mode,
url_ready_func rdy,
entity pass )

Definition at line 86 of file urllib.qc.

87{
88 entity e;
89 int i;
90 if (strstrofs(url, "://", 0) >= 0)
91 {
92 switch (mode)
93 {
94 case FILE_WRITE:
95 case FILE_APPEND:
96 // collect data to a stringbuffer for a POST request
97 // attempts to close will result in a reading handle
98
99 // create a writing end that does nothing yet
100 e = new_pure(url_single_fopen_file);
101 e.url_url = strzone(url);
102 e.url_content_type = "text/plain";
103 e.url_verb = "";
104 e.url_fh = URL_FH_CURL;
105 e.url_wbuf = buf_create();
106 if (e.url_wbuf < 0)
107 {
108 LOG_INFO("url_single_fopen: out of memory in buf_create");
109 rdy(e, pass, URL_READY_ERROR);
110 strfree(e.url_url);
111 delete(e);
112 return;
113 }
114 e.url_wbufpos = 0;
115 e.url_rbuf = -1;
116 e.url_ready = rdy;
117 e.url_ready_pass = pass;
118 rdy(e, pass, URL_READY_CANWRITE);
119 break;
120
121 case FILE_READ:
122 // read data only
123
124 // get slot for HTTP request
125 for (i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
126 if (url_fromid[i] == NULL) break;
127 if (i >= NUM_URL_ID)
128 {
129 for (i = 0; i < autocvar__urllib_nextslot; ++i)
130 if (url_fromid[i] == NULL) break;
132 {
133 LOG_INFO("url_single_fopen: too many concurrent requests");
135 return;
136 }
137 }
138
139 // GET the data
140 if (!crypto_uri_postbuf(url, i + MIN_URL_ID, string_null, string_null, -1, 0))
141 {
142 LOG_INFO("url_single_fopen: failure in crypto_uri_postbuf");
144 return;
145 }
146
147 // Make a dummy handle object (no buffers at
148 // all). Wait for data to come from the
149 // server, then call the callback
150 e = new_pure(url_single_fopen_file);
151 e.url_url = strzone(url);
152 e.url_fh = URL_FH_CURL;
153 e.url_rbuf = -1;
154 e.url_wbuf = -1;
155 e.url_ready = rdy;
156 e.url_ready_pass = pass;
157 e.url_id = i;
158 url_fromid[i] = e;
159
160 // make sure this slot won't be reused quickly even on map change
161 cvar_set("_urllib_nextslot", ftos((i + 1) % NUM_URL_ID));
162 break;
163 }
164 }
165 else if (url == "-")
166 {
167 switch (mode)
168 {
169 case FILE_WRITE:
170 case FILE_APPEND:
171 e = new_pure(url_single_fopen_stdout);
172 e.url_fh = URL_FH_STDOUT;
173 e.url_ready = rdy;
174 e.url_ready_pass = pass;
175 rdy(e, pass, URL_READY_CANWRITE);
176 break;
177 case FILE_READ:
178 LOG_INFO("url_single_fopen: cannot open '-' for reading");
180 break;
181 }
182 }
183 else
184 {
185 float fh = fopen(url, mode);
186 if (fh < 0)
187 {
189 return;
190 }
191 else
192 {
193 e = new_pure(url_single_fopen_file);
194 e.url_fh = fh;
195 e.url_ready = rdy;
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);
199 }
200 }
201}
const float FILE_READ
const float FILE_WRITE
const float FILE_APPEND
#define strstrofs
#define buf_create
float fopen(string filename, float mode)
const float URL_READY_CANREAD
Definition urllib.qh:17
const float URL_READY_CANWRITE
Definition urllib.qh:16

References autocvar__urllib_nextslot, buf_create, cvar_set(), entity(), FILE_APPEND, FILE_READ, FILE_WRITE, fopen(), ftos(), LOG_INFO, MIN_URL_ID, new_pure, NULL, NUM_URL_ID, pass, strfree, string_null, strstrofs, strzone(), URL_FH_CURL, URL_FH_STDOUT, url_fromid, URL_READY_CANREAD, URL_READY_CANWRITE, and URL_READY_ERROR.

Referenced by MX_Forget(), MX_Join(), MX_Leave(), MX_Messages(), MX_Nick(), MX_Say(), MX_Sync(), MX_Typing(), PlayerStats_PlayerBasic(), PlayerStats_PlayerDetail(), url_multi_fopen(), url_multi_ready(), XonoticServerToSTab_loadToS(), and XonoticToSDialog_loadXonoticToS().

◆ url_URI_Get_Callback()

ERASEABLE float url_URI_Get_Callback ( int id,
float status,
string data )

Definition at line 28 of file urllib.qc.

29{
30 if (id < MIN_URL_ID) return 0;
31 id -= MIN_URL_ID;
32 if (id >= NUM_URL_ID) return 0;
33 entity e = url_fromid[id];
34 if (!e) return 0;
35 if (e.url_rbuf >= 0 || e.url_wbuf >= 0)
36 {
37 LOG_INFOF("WARNING: handle %d (%s) has already received data?!?", id + NUM_URL_ID, e.url_url);
38 return 0;
39 }
40
41 // whatever happens, we will remove the URL from the list of IDs
42 url_fromid[id] = NULL;
43
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");
46
47 if (status == 0)
48 {
49 // WE GOT DATA!
50 float n, i;
51 n = tokenizebyseparator(data, "\n");
52 e.url_rbuf = buf_create();
53 if (e.url_rbuf < 0)
54 {
55 LOG_INFO("url_URI_Get_Callback: out of memory in buf_create");
56 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
57 strfree(e.url_url);
58 delete(e);
59 return 1;
60 }
61 e.url_rbufpos = 0;
62 if (e.url_rbuf < 0)
63 {
64 LOG_INFO("url_URI_Get_Callback: out of memory in buf_create");
65 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
66 strfree(e.url_url);
67 delete(e);
68 return 1;
69 }
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);
73 return 1;
74 }
75 else
76 {
77 // an ERROR
78 e.url_ready(e, e.url_ready_pass, -fabs(status));
79 strfree(e.url_url);
80 delete(e);
81 return 1;
82 }
83}
#define tokenizebyseparator
#define LOG_INFOF(...)
Definition log.qh:66
float fabs(float f)

References argv(), buf_create, entity(), error, fabs(), LOG_INFO, LOG_INFOF, MIN_URL_ID, NULL, NUM_URL_ID, strfree, tokenizebyseparator, URL_FH_CURL, url_fromid, URL_READY_CANREAD, and URL_READY_ERROR.

Referenced by URI_Get_Callback(), and URI_Get_Callback().

Variable Documentation

◆ autocvar__urllib_nextslot

int autocvar__urllib_nextslot

Definition at line 25 of file urllib.qc.

Referenced by url_fclose(), and url_single_fopen().

◆ url_attempt

int url_attempt

Definition at line 21 of file urllib.qc.

◆ url_content_type

string url_content_type

Definition at line 10 of file urllib.qc.

◆ url_fh

float url_fh

Definition at line 4 of file urllib.qc.

◆ URL_FH_CURL

const float URL_FH_CURL = -1

Definition at line 5 of file urllib.qc.

Referenced by url_fclose(), url_fgets(), url_fputs(), url_single_fopen(), and url_URI_Get_Callback().

◆ URL_FH_STDOUT

const float URL_FH_STDOUT = -2

Definition at line 6 of file urllib.qc.

Referenced by url_fclose(), url_fgets(), url_fputs(), and url_single_fopen().

◆ url_fromid

entity url_fromid[NUM_URL_ID]

Definition at line 24 of file urllib.qc.

Referenced by url_fclose(), url_single_fopen(), and url_URI_Get_Callback().

◆ url_id

float url_id

Definition at line 16 of file urllib.qc.

◆ url_mode

int url_mode

Definition at line 22 of file urllib.qc.

◆ url_rbuf

float url_rbuf

Definition at line 14 of file urllib.qc.

◆ url_rbufpos

float url_rbufpos

Definition at line 15 of file urllib.qc.

◆ url_ready

url_ready_func url_ready

Definition at line 17 of file urllib.qc.

◆ url_ready_pass

entity url_ready_pass

Definition at line 18 of file urllib.qc.

◆ url_url

string url_url

Definition at line 9 of file urllib.qc.

◆ url_verb

string url_verb

Definition at line 11 of file urllib.qc.

◆ url_wbuf

float url_wbuf

Definition at line 12 of file urllib.qc.

◆ url_wbufpos

float url_wbufpos

Definition at line 13 of file urllib.qc.