11 .url_ready_func url_ready;
12 .entity url_ready_pass;
14 #define MIN_URL_ID 128
16 entity url_fromid[NUM_URL_ID];
18 float url_URI_Get_Callback(float id, float status, string data)
29 if(e.url_rbuf >= 0 || e.url_wbuf >= 0)
31 print(sprintf("WARNING: handle %d (%s) has already received data?!?\n", id + NUM_URL_ID, e.url_url));
35 // whatever happens, we will remove the URL from the list of IDs
36 url_fromid[id] = world;
42 n = tokenizebyseparator(data, "\n");
43 e.url_rbuf = buf_create();
47 print("buf_create: out of memory\n");
48 e.url_ready(e, e.url_ready_pass, URL_READY_ERROR);
52 for(i = 0; i < n; ++i)
53 bufstr_set(e.url_rbuf, i, argv(i));
54 e.url_ready(e, e.url_ready_pass, URL_READY_CANREAD);
60 e.url_ready(e, e.url_ready_pass, -status);
67 void url_fopen(string url, float mode, entity pass, url_ready_func rdy)
71 if(strstrofs(url, "://", -1))
77 // collect data to a stringbuffer for a POST request
78 // attempts to close will result in a reading handle
80 e.classname = "url_fopen_file";
81 e.url_url = strzone(url);
83 e.url_wbuf = buf_create();
86 print("buf_create: out of memory\n");
89 rdy(world, pass, URL_READY_ERROR);
94 rdy(e, pass, URL_READY_CANWRITE);
99 for(i = 0; i < NUM_URL_ID; ++i)
100 if(url_fromid[i] == world)
104 rdy(world, pass, URL_READY_ERROR);
109 e.classname = "url_fopen_file";
110 e.url_url = strzone(url);
114 if(!uri_get(url, i + MIN_URL_ID))
116 print("uri_get: failed\n");
117 strunzone(e.url_url);
119 rdy(world, pass, URL_READY_ERROR);
123 e.url_ready_pass = pass;
132 fh = fopen(url, mode);
135 rdy(world, pass, URL_READY_ERROR);
141 e.classname = "url_fopen_file";
143 if(mode == FILE_READ)
144 rdy(e, pass, URL_READY_CANREAD);
146 rdy(e, pass, URL_READY_CANWRITE);
151 void url_fclose(entity e, entity pass, url_ready_func rdy)
159 for(i = 0; i < NUM_URL_ID; ++i)
160 if(url_fromid[i] == world)
164 rdy(e, pass, URL_READY_ERROR);
166 strunzone(e.url_url);
171 if(!uri_postbuf(e.url_url, e.url_id + MIN_URL_ID, "text/plain", "\n", e.url_wbuf))
173 rdy(e, pass, URL_READY_ERROR);
175 strunzone(e.url_url);
183 e.url_ready_pass = pass;
189 // we have READ all data
190 rdy(e, pass, URL_READY_CLOSED);
192 strunzone(e.url_url);
200 rdy(e, pass, URL_READY_CLOSED); // closing creates no reading handle
206 string url_fgets(entity e)
212 s = bufstr_get(e.url_rbuf, e.url_rbufpos);
219 return fgets(e.url_fh);
224 void url_fputs(entity e, string s)
229 bufstr_set(e.url_wbuf, e.url_wbufpos, s);