11 .url_ready_func url_ready;
12 .entity url_ready_pass;
14 #define MIN_URL_ID 128
16 entity url_fromid[NUM_URL_ID];
17 float autocvar__urllib_nextslot;
19 float url_URI_Get_Callback(float id, float status, string data)
30 if(e.url_rbuf >= 0 || e.url_wbuf >= 0)
32 print(sprintf("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url));
36 // whatever happens, we will remove the URL from the list of IDs
37 url_fromid[id] = world;
43 n = tokenizebyseparator(data, "\n");
44 e.url_rbuf = buf_create();
48 print("buf_create: out of memory\n");
49 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
53 for(i = 0; i < n; ++i)
54 bufstr_set(e.url_rbuf, i, argv(i));
55 e.url_ready(e, e.url_ready_pass, URL_READY_CANREAD);
61 e.url_ready(e, e.url_ready_pass, -status);
68 void url_fopen(string url, float mode, url_ready_func rdy, entity pass)
72 if(strstrofs(url, "://", -1))
78 // collect data to a stringbuffer for a POST request
79 // attempts to close will result in a reading handle
81 e.classname = "url_fopen_file";
82 e.url_url = strzone(url);
84 e.url_wbuf = buf_create();
87 print("buf_create: out of memory\n");
88 rdy(e, pass, URL_READY_ERROR);
95 rdy(e, pass, URL_READY_CANWRITE);
100 for(i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
101 if(url_fromid[i] == world)
105 for(i = 0; i < autocvar__urllib_nextslot; ++i)
106 if(url_fromid[i] == world)
108 if(i >= autocvar__urllib_nextslot)
110 rdy(world, pass, URL_READY_ERROR);
116 e.classname = "url_fopen_file";
117 e.url_url = strzone(url);
121 if(!uri_get(url, i + MIN_URL_ID))
123 rdy(e, pass, URL_READY_ERROR);
124 strunzone(e.url_url);
129 e.url_ready_pass = pass;
132 cvar_set("_urllib_nextslot", ftos(mod(i + 1, NUM_URL_ID)));
139 fh = fopen(url, mode);
142 rdy(world, pass, URL_READY_ERROR);
148 e.classname = "url_fopen_file";
150 if(mode == FILE_READ)
151 rdy(e, pass, URL_READY_CANREAD);
153 rdy(e, pass, URL_READY_CANWRITE);
158 void url_fclose(entity e, url_ready_func rdy, entity pass)
166 for(i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
167 if(url_fromid[i] == world)
171 for(i = 0; i < autocvar__urllib_nextslot; ++i)
172 if(url_fromid[i] == world)
174 if(i >= autocvar__urllib_nextslot)
177 rdy(e, pass, URL_READY_ERROR);
178 strunzone(e.url_url);
184 if(!uri_postbuf(e.url_url, e.url_id + MIN_URL_ID, "text/plain", "\n", e.url_wbuf))
187 rdy(e, pass, URL_READY_ERROR);
188 strunzone(e.url_url);
196 e.url_ready_pass = pass;
199 cvar_set("_urllib_nextslot", ftos(mod(i + 1, NUM_URL_ID)));
203 // we have READ all data
204 rdy(e, pass, URL_READY_CLOSED);
206 strunzone(e.url_url);
214 rdy(e, pass, URL_READY_CLOSED); // closing creates no reading handle
220 string url_fgets(entity e)
226 s = bufstr_get(e.url_rbuf, e.url_rbufpos);
233 return fgets(e.url_fh);
238 void url_fputs(entity e, string s)
243 bufstr_set(e.url_wbuf, e.url_wbufpos, s);