11 .url_ready_func url_ready;
12 .entity url_ready_pass;
14 entity url_fromid[NUM_URL_ID];
15 float autocvar__urllib_nextslot;
17 float url_URI_Get_Callback(float id, float status, string data)
28 if(e.url_rbuf >= 0 || e.url_wbuf >= 0)
30 print(sprintf("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url));
34 // whatever happens, we will remove the URL from the list of IDs
35 url_fromid[id] = world;
41 n = tokenizebyseparator(data, "\n");
42 e.url_rbuf = buf_create();
46 print("buf_create: out of memory\n");
47 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
51 for(i = 0; i < n; ++i)
52 bufstr_set(e.url_rbuf, i, argv(i));
53 e.url_ready(e, e.url_ready_pass, URL_READY_CANREAD);
59 e.url_ready(e, e.url_ready_pass, -status);
66 void url_fopen(string url, float mode, url_ready_func rdy, entity pass)
70 if(strstrofs(url, "://", 0) >= 0)
76 // collect data to a stringbuffer for a POST request
77 // attempts to close will result in a reading handle
79 e.classname = "url_fopen_file";
80 e.url_url = strzone(url);
82 e.url_wbuf = buf_create();
85 print("buf_create: out of memory\n");
86 rdy(e, pass, URL_READY_ERROR);
93 rdy(e, pass, URL_READY_CANWRITE);
98 for(i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
99 if(url_fromid[i] == world)
103 for(i = 0; i < autocvar__urllib_nextslot; ++i)
104 if(url_fromid[i] == world)
106 if(i >= autocvar__urllib_nextslot)
108 rdy(world, pass, URL_READY_ERROR);
114 e.classname = "url_fopen_file";
115 e.url_url = strzone(url);
119 if(!uri_get(url, i + MIN_URL_ID))
121 rdy(e, pass, URL_READY_ERROR);
122 strunzone(e.url_url);
127 e.url_ready_pass = pass;
130 cvar_set("_urllib_nextslot", ftos(mod(i + 1, NUM_URL_ID)));
137 fh = fopen(url, mode);
140 rdy(world, pass, URL_READY_ERROR);
146 e.classname = "url_fopen_file";
148 if(mode == FILE_READ)
149 rdy(e, pass, URL_READY_CANREAD);
151 rdy(e, pass, URL_READY_CANWRITE);
156 void url_fclose(entity e, url_ready_func rdy, entity pass)
164 for(i = autocvar__urllib_nextslot; i < NUM_URL_ID; ++i)
165 if(url_fromid[i] == world)
169 for(i = 0; i < autocvar__urllib_nextslot; ++i)
170 if(url_fromid[i] == world)
172 if(i >= autocvar__urllib_nextslot)
175 rdy(e, pass, URL_READY_ERROR);
176 strunzone(e.url_url);
181 print(ftos(i), "\n");
183 if(!uri_postbuf(e.url_url, i + MIN_URL_ID, "text/plain", "", e.url_wbuf))
186 rdy(e, pass, URL_READY_ERROR);
187 strunzone(e.url_url);
195 e.url_ready_pass = pass;
198 cvar_set("_urllib_nextslot", ftos(mod(i + 1, NUM_URL_ID)));
202 // we have READ all data
203 rdy(e, pass, URL_READY_CLOSED);
205 strunzone(e.url_url);
213 rdy(e, pass, URL_READY_CLOSED); // closing creates no reading handle
219 string url_fgets(entity e)
225 s = bufstr_get(e.url_rbuf, e.url_rbufpos);
232 return fgets(e.url_fh);
237 void url_fputs(entity e, string s)
242 bufstr_set(e.url_wbuf, e.url_wbufpos, s);